我使用python 2.7,正在while循环中从文件读取数据。当我成功读取行时,我想从文件中删除该行,但是我不知道该怎么做-一种有效的方式,这样我就不会浪费很多CPU。
read = open("data.csv", 'r')
for row in read:
#code......
if send == True:
-->delete sent line from file, and continue to loop
答案 0 :(得分:0)
我认为最好和最快的选择是将文件重写到其他位置,而无需删除行。
只要具有适当的否定条件即可确定要保留的行。
with open("data.csv", 'r'):
with open("output.txt","w") as output:
for row in read:
#code......
if not send == True:
-->delete sent line from file, and continue to loop
output.write(line)
答案 1 :(得分:0)
在做磁盘IO时,您不必担心cpu的使用-与几乎任何内存/ cpu操作相比,磁盘IO的速度都很慢。
有两种从文件中间删除的策略:
编写所有行以保留到辅助文件,然后将辅助文件重命名为原始文件名。
将文件的其余部分(尾部)复制到要删除的行的开头,然后从文件末尾截断x
个字节(其中x
等于您要删除的行的长度。
通常首选数字1,因为它更容易使用并且不需要任何锁定。
玛雅克·波瓦尔(Mayank Porwal)为您提供了第一大策略。这是实施策略2的方法:
# open the file for both reading and writing in binary mode ('rb+')
with open('rmline.txt', 'rb+') as fp:
while 1:
pos = fp.tell() # remember the starting position of the next line to read
line = fp.readline()
if not line:
break # we reached the end of the file
if should_line_be_skipped(line): # only you know what to skip :-)
rest = fp.read() # read the rest of the file
fp.seek(pos) # go to the start position of the line to remove
fp.write(rest) # write the rest of the file over the line to be removed
fp.truncate() # truncates at current position (end of file - len(line))
fp.seek(pos) # return to where the next line is after deletion so we can continue the while loop