我有一个像这样的文件
abc abc+bcd+cdf adf;asfg
找到'+'或';'并在新行中打印字符串的剩余部分!
输出
abc abc bcd cdf adf asfg
只有Python!
答案 0 :(得分:1)
您可以使用re.sub
:
演示:
>>> strs = "abc+bcd+cdf"
>>> re.sub(r'\+|;','\n',strs)
'abc\nbcd\ncdf'
代码:
>>> import re
>>> with open('abc') as f:
... for line in f:
... line = re.sub(r'\+|;','\n',line.strip())
... print line
...
abc
abc
bcd
cdf
adf
asfg
答案 1 :(得分:1)
使用re.sub
import re
with open("myfile","rt") as f:
for line if f:
print re.sub("[;+]", "\n", line.strip())