gpt4 book ai didi

python - 在python中将行号添加到字符串中

转载 作者:行者123 更新时间:2023-11-28 20:45:56 25 4
gpt4 key购买 nike

我有一个以下格式的文本文件:

"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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com