另一种方法是
re.split
.在单词上拆分,后跟冒号,并可选地用空格包围,捕获单词:
>>> s = 'address: C/O John Smith @ Building X, S/W city: new york state: new york population: 500000'
>>> re.split('\s*(\w+):\s*',s)
['', 'address', 'C/O John Smith @ Building X, S/W', 'city', 'new york', 'state', 'new york', 'population', '500000']
开头会有一个空字符串,
zip
向上交替设置键和值,并转换为
dict
:
>>> x=re.split('\s*(\w+):\s*',s)
>>> dict(zip(x[1::2],x[2::2]))
{'address': 'C/O John Smith @ Building X, S/W', 'city': 'new york', 'state': 'new york', 'population': '500000'}