将文件的某些部分“移动”到另一个文件
问题描述:
假设我有一个包含48,222行的文件。然后我给出一个指数值,比方说21,000。将文件的某些部分“移动”到另一个文件
Python中是否有任何方式可以从索引21,000开始“移动”文件的内容,现在我有两个文件:原始文件和新文件。但是原来的一条现在有21,000条线和新的27,222条线。
我读这post它使用的分区,是相当描述我想要什么:
with open("inputfile") as f:
contents1, sentinel, contents2 = f.read().partition("Sentinel text\n")
with open("outputfile1", "w") as f:
f.write(contents1)
with open("outputfile2", "w") as f:
f.write(contents2)
只是(1)它使用“哨兵文本”作为分隔符,(2),它创建了两个新的文件,需要我删除旧文件。截至目前,我这样做的方式是这样的:
for r in result.keys(): #the filenames are in my dictionary, don't bother that
f = open(r)
lines = f.readlines()
f.close()
with open("outputfile1.txt", "w") as fn:
for line in lines[0:21000]:
#write each line
with open("outputfile2.txt", "w") as fn:
for line in lines[21000:]:
#write each line
这是一个相当手动的工作。有没有内置或更有效的方法?
答
您还可以使用writelines()将从0到20999行的切片列表转储到一个文件中,将另一个切片列表从21000转储到另一个文件中。
with open("inputfile") as f:
content = f.readlines()
content1 = content[:21000]
content2 = content[21000:]
with open("outputfile1.txt", "w") as fn1:
fn1.writelines(content1)
with open('outputfile2.txt','w') as fn2:
fn2.writelines(content2)
+0
感谢您的回应。当然是另一种选择。Upvoted。 – Ian
如果所有的线条都是一样的长度,你可以用'seek',如果不是我想的内存明智倒不如循环21000倍'的ReadLine()'和传球然后加载所有行内存与'readlines'(在你提到的第二个选项) –
@UShaShalit不,他们很不幸,每行的长度不一样... :(它大约是每个文件1.5-3 MB。到目前为止,我花了250ms时间来打开和读取一个文件,问题是,我有这样的数千个文件,我想知道是否有更高效的方法 – Ian