Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - How can i indicate the strings

 long =   """ADDRESS: Some place in the world
    TEL: 555 5555 5555 TYPE: Apartment/High
    Data Accuracy: Very heigh building with plenty of corroborating data"""

Lets assume i have a long string like that, i would like to parse them up and add to my dict

mydict = {'Adress':[],'Tel':[],'Type':[],'Data Accuracy':[]}

I have tried this

import re
x = re.split('ADRESS',long)

However this doesnt enough i would like parse them into 4 pieces and add them into mydict.x = re.split('ADRESS',long) this only parses it to one peace.

question from:https://stackoverflow.com/questions/66049790/how-can-i-indicate-the-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll still have a little bit of clean up to do i.e. remove front and trailing white space so strip() but this will work as asked:

import re

long = """ADDRESS: Some place in the world
    TEL: 555 5555 5555 TYPE: Apartment/High
    Data Accuracy: Very heigh building with plenty of corroborating data"""

x = re.split(r"[a-zA-Z]+:",long)
print(x)
# ['', ' Some place in the world
    ', ' 555 5555 5555 ', ' Apartment/High
    Data ', ' Very heigh building with plenty of corroborating data']

clean = []
for item in x:
    if item != "":
        clean.append(item.split('
')[0].strip())
print(clean)
# ['Some place in the world', '555 5555 5555', 'Apartment/High', 'Very heigh building with plenty of corroborating data']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...