【发布时间】:2016-11-30 21:47:01
【问题描述】:
这是我用来替换文件中某些文本的代码:
for line in fileinput.input(the_file,inplace=True):
sys.stdout.write(line.re.sub('Begin.*?end', new_data))
这给了我以下错误:
AttributeError: 'str' 对象没有属性 're'
我想也许我可以像这样直接使用re.subpart:
sys.stdout.write(re.sub('Begin.*?end', new_data))
但这给了我错误:
TypeError: sub() 缺少 1 个必需的位置参数:'string'
如何在 Python 中使用带有 fileinput 的正则表达式来正确搜索和替换文本?
【问题讨论】:
标签: python regex python-3.x python-3.5