2024-07-06 23:38:17 发布
网友
我有一个文本文件,我需要打印它以前的单词。the文本文件包含为:
Sparrow=beak Hen=nest Honey=comb
我需要输出为:
编码:
import re with open('qwert.txt', 'r') as f: for line in f: res = re.findall(r'(?:=(\w-))', line) if res: print res
我没有得到输出,请帮忙!在
使用正前瞻断言(?=...):
(?=...)
import re with open('qwert.txt', 'r') as f: for line in f: for res in re.findall(r'\w+(?==\w)', line): # Match word characters (`\w+`) followed by `=` and word character. print res
如果字符串格式有点严格,那么可以使用split,它不仅可以获取键,还可以获取值。在
line = "Hen=nest Honey=comb" keys = [key for key, value in [token.split('=') for token in line.split(' ')]]
然后把钥匙打印出来。在
使用正前瞻断言
(?=...)
:如果字符串格式有点严格,那么可以使用split,它不仅可以获取键,还可以获取值。在
然后把钥匙打印出来。在
相关问题 更多 >
编程相关推荐