我有一份包含以下数据的清单:
["asdf mkol ghth", "dfcf 5566 7766", "7uy7 jhjh ffvf"]
我想在python中使用正则表达式来获得这样的元组列表
[("asdf", "mkol ghth"),("dfcf", "5566 7766"),("7uy7", "jhjh ffvf")]
我尝试使用re.split,但出现了一个错误,说明要解包的值太多。以下是我的代码:
logTuples = [()]
for log in logList:
(logid, logcontent) = re.split(r"(\s)", log)
logTuples.append((logid, logcontent))
正则表达式在这里是多余的:
印刷品:
根据文件:
https://docs.python.org/3/library/re.html
有2个空格,因此有3个项目
如果所有日志条目都有3个用空格分隔的项,并且您总是将它们组织为(1,2+“”+3),则不需要使用正则表达式将它们格式化为:
相关问题 更多 >
编程相关推荐