Python 如何指示字符串
假设我有一个这样的长字符串,我想解析它们并添加到我的dict中Python 如何指示字符串,python,string,Python,String,假设我有一个这样的长字符串,我想解析它们并添加到我的dict中 long = """ADDRESS: Some place in the world TEL: 555 5555 5555 TYPE: Apartment/High Data Accuracy: Very heigh building with plenty of corroborating data""" 我试过这个 mydict = {'Adre
long = """ADDRESS: Some place in the world
TEL: 555 5555 5555 TYPE: Apartment/High
Data Accuracy: Very heigh building with plenty of corroborating data"""
我试过这个
mydict = {'Adress':[],'Tel':[],'Type':[],'Data Accuracy':[]}
然而,这还不够,我想将它们解析成4个部分并添加到mydict中。
x=re.split('address',long)
这只会将其解析为一个和平。您仍然需要做一点清理,即删除前面和后面的空白,以便strip()
但这将按要求工作:
import re
x = re.split('ADRESS',long)
您仍然需要进行一些清理,即删除前面和后面的空白,以便
strip()
,但这将按要求进行:
import re
x = re.split('ADRESS',long)
不使用正则表达式
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\n ', ' 555 5555 5555 ', ' Apartment/High\n Data ', ' Very heigh building with plenty of corroborating data']
clean = []
for item in x:
if item != "":
clean.append(item.split('\n')[0].strip())
print(clean)
# ['Some place in the world', '555 5555 5555', 'Apartment/High', 'Very heigh building with plenty of corroborating data']
输出:
long = """ADDRESS: Some place in the world
TEL: 555 5555 5555 TYPE: Apartment/High
Data Accuracy: Very heigh building with plenty of corroborating data"""
indicators = ["ADDRESS", "TEL", "TYPE", "Data Accuracy"]
dict_ = dict()
for indicator in reversed(indicators):
long, value = long.split(indicator + ":")
dict_[indicator] = value.strip()
print(dict_)
不使用正则表达式
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\n ', ' 555 5555 5555 ', ' Apartment/High\n Data ', ' Very heigh building with plenty of corroborating data']
clean = []
for item in x:
if item != "":
clean.append(item.split('\n')[0].strip())
print(clean)
# ['Some place in the world', '555 5555 5555', 'Apartment/High', 'Very heigh building with plenty of corroborating data']
输出:
long = """ADDRESS: Some place in the world
TEL: 555 5555 5555 TYPE: Apartment/High
Data Accuracy: Very heigh building with plenty of corroborating data"""
indicators = ["ADDRESS", "TEL", "TYPE", "Data Accuracy"]
dict_ = dict()
for indicator in reversed(indicators):
long, value = long.split(indicator + ":")
dict_[indicator] = value.strip()
print(dict_)
使用正则表达式匹配指示符。我如何使用它?您知道什么方法吗?请阅读www.regular-expression.info上的教程使用正则表达式匹配指示符。我如何使用它?您知道什么方法吗?请阅读www.regular-expression.info上的教程。谢谢,这也很有帮助!!谢谢,这也很有帮助!!我有一个问题,为什么当我们不在a到z括号中传递时,它不起作用。[a-zA-Z]这很好,但是“[ADDRESS TEL TYPE Data Accurance]+:”当我像这样传入时,它不作为a到Z括号[a-zA-Z]这是因为它是一个正则表达式,不像Python中的列表,所以在这个上下文中,它只匹配字符a到z和a到z。通过添加您所拥有的内容,它不再是一个匹配项,因为没有所谓的已添加内容。如果你正在做一个单词匹配,它不会像这样,它会有a/b,你需要添加一个or。我建议阅读关于regex的文章,以便更好地理解它。你有没有合适的文章或网站来学习regex?谢谢,我很感激。我有一个问题,为什么它不能像我们不在a到z括号中传递时那样工作。[a-zA-Z]这很好,但是“[ADDRESS TEL TYPE Data Accurance]+:”当我像这样传入时,它不作为a到Z括号[a-zA-Z]这是因为它是一个正则表达式,不像Python中的列表,所以在这个上下文中,它只匹配字符a到z和a到z。通过添加您所拥有的内容,它不再是一个匹配项,因为没有所谓的已添加内容。如果你正在做一个单词匹配,它不会像这样,它会有a/b,你需要添加一个or。我建议你阅读一些关于regex的文章来更好地理解它。你有没有合适的文章或网站来学习regex?谢谢,我很感激。