Python Forum
extract from strings some parts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: extract from strings some parts (/thread-19571.html)



extract from strings some parts - dervast - Jul-04-2019

Hi,
I have a data frame where a specific columns is filled in with long strings. I want for each entry there to extract a specific word if that word has some of the following attributes:

-word ends with strasse. Then export the whole word until the comma.
-word starts with a capital A. The export the whole word until the comma.
-words ends gasse. Then export the whole word until the comma.
-word ends with weg. The export the whole word until the comma.
-word starts with a capital A, return the whole word until the comma.

For example for the string
"A12, Thörishaus, Köniz, Verwaltungskreis"

return A12 (last rule)

For example for the string
"Stapfenstrasse, Köniz, Verwaltungskreis"

return Stapfenstrasse (first rule).

How I can do something like that.

So for each entry in dataframe column check if one of the rules apply and then export the string needed.

Can you please advice?

Thanks
Alex


RE: extract from strings some parts - Axel_Erfurt - Jul-04-2019

split the string into list

mstring = "A12, Thörishaus, Köniz, Verwaltungskreis"
### get List from string
mlist  = mstring.split(",")
### get first value from list
print(mlist[0])
Result:
Output:
A12