我不确定为什么在python中使用带有Regex的Match()时,在每个输出的末尾都会得到一个额外的逗号。有任何想法吗 ?感谢。
我的代码:
import re
yyyyfile = open("yyyy.txt",'w')
text = open('final.txt')
for line in text:
x = re.match('.*?^([0-9][0-9][0-9][0-9])$.*', line)
if x:
print >> yyyyfile, x.groups()
当前输出:
( '1573')
( '1595')
( '1929')
( '1933')
期望的输出:
( '1573')
( '1595')
( '1929')
( '1933')
答案 0 :(得分:4)
这是因为re.match的groups()的输出是您捕获的所有组。这是一个元组
请改用x.group(1)
或其他。
答案 1 :(得分:0)
我不知道yyyy.txt
内部是什么以及为什么要打印它,但假设final.txt
包含文本,您应该打开文件:
with open('final.txt', 'r') as f:
text = f.read()
解析的工作原理如下:
import re
text = '''1235, dfx as nxcn 1229 nxcn 32 4 0 9877'''
matches = re.findall('(\d{4})', text)
for match in matches:
print(match)
答案 2 :(得分:0)
您没有获得额外的逗号,因为您使用匹配。你得到这个逗号的原因是你打印出元组。您可以像这样实现所需的结果
print >> yyyyfile, '(%s)' % x.group(1)