有这样的事情:
keys = ["Name:", "Date:", "Time:sec:", "Room"]
string = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
如何获得如下字典:
dict1 = {"Name" : "Bob", "Date" : "1/3", "Time:sec" : "3:00:00", "Room" : "A1"}
删除冒号是可选的。
我可以使用re.split()
,.join()
和map()
完全删除字符串中的键,但我想创建一个字典。
答案 0 :(得分:4)
只是一种正则表达方式......
dict(zip(keys, re.match('(.*)'.join(keys + [""]), string).groups()))
演示:
>>> if 1:
import re
keys = ["Name:", "Date:", "Time:sec:", "Room"]
string = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
dict(zip(keys, re.match('(.*)'.join(keys + [""]), string).groups()))
{'Name:': 'Bob', 'Date:': '1/3', 'Time:sec:': '3:00:00', 'Room': 'A1'}
答案 1 :(得分:3)
鉴于您的意见:
keys = ["Name:", "Date:", "Time:sec:", "Room"]
string = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
您可以使用保留拆分键本身的正则表达式拆分它,例如:
split = re.split('({})'.format('|'.join(re.escape(k) for k in keys)), string)
# ['', 'Name:', 'Bob', 'Date:', '1/3', 'Time:sec:', '3:00:00', 'Room', 'A1']
然后,使用dict
来压缩相应的切片(我们从1开始,因为前导空匹配),例如:
dct = dict(zip(split[1::2], split[2::2]))
# {'Date:': '1/3', 'Name:': 'Bob', 'Time:sec:': '3:00:00', 'Room': 'A1'}
答案 2 :(得分:1)
在循环中使用.split()
:
keys = ["Name:", "Date:", "Time:sec:", "Room"]
s = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
values = []
temp = s.split(keys[0])[-1]
for key in keys[1:]:
val, temp = temp.split(key)
values.append(val)
values.append(temp)
dict1 = dict(zip(keys, values))
print(dict1)
输出:
{'Name:': 'Bob', 'Date:': '1/3', 'Time:sec:': '3:00:00', 'Room': 'A1'}
答案 3 :(得分:0)
一线方法,
In [40]: dict(zip(keys,[string.split(j)[-1].split(keys[-1])[0] if i == (len(keys) - 1) else string.split(j)[-1].split(keys[i+1])[0] for i,j in enumerate(keys)]))
Out[40]: {'Date:': '1/3', 'Name:': 'Bob', 'Room': 'A1', 'Time:sec:': '3:00:00'}
我知道这是非常复杂的方法:),只是为了显示不同的答案选项。
答案 4 :(得分:0)
我们可以用rowbreak替换字符串中的键(要拆分的东西)。然后执行一个字典(zip(...
keys = ["Name:", "Date:", "Time:sec:", "Room"]
string = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
for key in keys:
string = string.replace(key,"\n")
d = dict(zip(keys,string.split('\n')[1:])) # 1: to handle first row break
d等于:
{'Date:': '1/3', 'Name:': 'Bob', 'Room': 'A1', 'Time:sec:': '3:00:00'}
答案 5 :(得分:0)
你可以试试这个:
import re
keys = ["Name:", "Date:", "Time:sec:", "Room"]
string = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
new_data = dict(zip(map(lambda x:x[:-1], keys), filter(None, re.split('\*', re.sub('|'.join(keys), '*', string)))))
输出:
{'Date': '1/3', 'Time:sec': '3:00:00', 'Name': 'Bob', 'Roo': 'A1'}
答案 6 :(得分:0)
这是我寻找答案的旅程
In [17]: import re
In [18]: keys = ["Name:", "Date:", "Time:sec:", "Room"]
In [19]: string = "Name:BobDate:1/3Time:sec:3:00:00RoomA1"
In [20]: separators = '|'.join(keys)
In [21]: separators
Out[21]: 'Name:|Date:|Time:sec:|Room'
In [22]: re.split(separators, string)
Out[22]: ['', 'Bob', '1/3', '3:00:00', 'A1']
In [23]: re.split(separators, string)[1:]
Out[23]: ['Bob', '1/3', '3:00:00', 'A1']
In [24]: values = re.split(separators, string)[1:]
In [25]: dict(zip(keys, values))
Out[25]: {'Date:': '1/3', 'Name:': 'Bob', 'Room': 'A1', 'Time:sec:': '3:00:00'}
In [26]: dict1 = dict(zip(keys, values))
re.split
中创建要使用的分隔符列表。管道符号(|
)表示" 或"在正则表达式dict1