我有一个以下格式的文本文件:
"This is record #1"
"This is record #2"
"This is record #3"
我需要以下格式的输出:
Line number (1) --\t-- "This is Record # 1"
2-- \t-- "This is Record # 2"
3-- \t-- "This is Record # 3"
当前代码:
f = open("C:\input.txt","r")
write_file = open("C:\output.txt","r+")
while True:
line = f.readline()
write_file.write(line)
if not line : break
write_file.close()
f.close()
最佳答案
尝试以这种方式遍历您的文件:
f = open('workfile', 'r')
for num,line in enumerate(f):
print(num+" "+line)
关于python - 在python中将行号添加到字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21975443/