我有一个类似示例的“test.txt”文件,其中包含:
--
a
--
b
--
c
和我的 Python 代码:
x = ['a','b','c']
i=1
with open("test.txt", "r") as fin:
with open("result.log", "w") as fout:
for line in fin:
if line.startswith(' --'):
fout.write(line.replace(' --','use {}'.format(str(x[i-1]))))
i+=i
else:
fout.write(line)
但结果是:
fout.write(line.replace(' --','use {}'.format(str(x[i-1]))))
IndexError: list index out of range
应该工作......有人可以帮助我吗?我想要结果:
use a
a
use b
b
use c
c