我试着用字母和数字来拆分一些行,但是我不能想出合适的正则表达式。你知道吗
行的格式类似于unit=value+unit,例如:
width = 3.45cm
height = 2m
width = 2mm
height = 6.67m
我想为每个名称、值和单位分别获得一个输出,这是我所做的:
line = infoData.readline()
names = []
values = []
units = []
while line:
if "=" in line:
names.append(line[0:line.index("=")])
m = re.search('\d+', line[line.index("="):len(line)])
values.append(int(m.group()))
m = re.search('\D+[^=\n\.]', line[line.index("="):len(line)])
units.append(m.group())
line = infoData.readline()
else:
line = infoData.readline()
我唯一想要的就是名字。。。。你知道吗
你把事情搞得太复杂了。我会用:
对于提供字典列表的示例数据:
而不是3个单独的列表。你知道吗
正则表达式演示:
Debuggex Demo
编辑:如果您正在寻找从正则表达式中获取词典的方法,可以这样做
输出
相关问题 更多 >
编程相关推荐