有没有办法只用正则表达式进行整数增量替换。
这是问题所在,我的文本文件包含 1 000 000 行,全部以 % 开头
我想使用正则表达式逐步将 # 替换为整数。
input:
% line one
% line two
% line three
...
output:
1 line one
2 line two
3 line three
...
最佳答案
n = 1
with open('sourcefile.txt') as input:
with open('destination.txt', 'w') as output:
for line in input:
if line.startswith('%'):
line = str(n) + line[1:]
n += 1
output.write(line)
关于c# - 正则表达式 - 增量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3185669/