我正在尝试将输出列表写入文件,但仅从列表中获取最后一行以实际写入文件。如何更改我的代码以捕获所有内容?
import os
slices = [(0,3), (12,28), (29,31), (32,33), (34,38), (130,144), (39,41), (32,33), (42,55), (58,64), (65,72), (73,74), (75,78), (79,84), (99,102)]
File = r"E:\file.txt"
outfile = r"E:\file_output.txt"
with open(File) as sourcefile:
for line in sourcefile:
result = [line[slice(*slc)] for slc in slices]
myfile = open(outfile, 'w')
for lines in result:
myfile.write("%s\t" % lines)
myfile.close()