简体   繁体   中英

How to re.sub european street adress to remove letters/digits

I'm working with a df where streets must be transformed into the following:

Streets:

A. Jakšto g. 2
Stumbrų g. 26A
M. K. Paco g. 19
Birželio 23-iosios g. 15
Grigiškių m. Kovo 11-osios g. 43
Laisvės pr. 87

Would need to be transformed to:

A. Jakšto g.
Stumbrų g.
M. K. Paco g.
Birželio 23-iosios g.
Grigiškių m. Kovo 11-osios g.
Laisvės pr

.

Yes, I know this isn't a place where someone could do the work for me - everything is Google'able, but I'm feeling really stuck here, even while reading the documentations ^^

Looks like you need to remove the last string in your text.

Demo:

s = """A. Jakšto g. 2
Stumbrų g. 26A
M. K. Paco g. 19
Birželio 23-iosios g. 15
Grigiškių m. Kovo 11-osios g. 43
Laisvės pr. 87"""

print("\n".join(" ".join(i.split()[:-1]) for i in s.splitlines()))

Output:

A. Jakšto g.
Stumbrų g.
M. K. Paco g.
Birželio 23-iosios g.
Grigiškių m. Kovo 11-osios g.
Laisvės pr.

Or using Regex.

Ex:

import re  

s = """A. Jakšto g. 2A
Stumbrų g. 26A
M. K. Paco g. 19
Birželio 23-iosios g. 15
Grigiškių m. Kovo 11-osios g. 43
Laisvės pr. 87"""

print(re.sub(r"(\d+[A-Za-z]?)$", "", s, flags=re.M))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM