with open('lab07_python.txt', 'r') as old:
lines = old.readlines()
with open('david.txt', 'w') as new:
for i, line in enumerate(lines):
new.write("%d %s\n" % (i, line))
infile = open('infile.txt','r')
outfile = open('outfile.txt', 'w+')
line_number = 0
for line in infile.readlines():
outfile.write(f'{line_number} {line}')
line_number += 1
# infile text
# line 0
# line 1
# line 2
# line 3
# line 4
# line 5
# outfile text
# 0 line 0
# 1 line 1
# 2 line 2
# 3 line 3
# 4 line 4
# 5 line 5
total = 0
with open('lab07_python.txt', 'r') as orig:
lines = orig.readlines()
for line in lines:
total = total +1
new = '%d %s' % (total, line)
david.write(new)
您需要遍历旧文件的行,例如:
这是完成这项任务的一种方法
通过添加一些字符串格式解决:
相关问题 更多 >
编程相关推荐