性能-python文本文件编辑2GB文件
我正在Python3中运行以下代码,以接收性能-python文本文件编辑2GB文件,python,performance,text,Python,Performance,Text,我正在Python3中运行以下代码,以接收.txt文件,每隔一行编辑一次,并存储编辑后的.txt文件。它适用于小文件,但我的文件是~2GB,而且需要的时间太长 有人对如何修改代码以提高效率和速度有什么建议吗 newData = "" i=0 run=0 j=0 k=1 seqFile = open('temp100.txt', 'r') seqData = seqFile.readlines() while i < 14371315: sLine = seqData[j]
.txt
文件,每隔一行编辑一次,并存储编辑后的.txt
文件。它适用于小文件,但我的文件是~2GB,而且需要的时间太长
有人对如何修改代码以提高效率和速度有什么建议吗
newData = ""
i=0
run=0
j=0
k=1
seqFile = open('temp100.txt', 'r')
seqData = seqFile.readlines()
while i < 14371315:
sLine = seqData[j]
editLine = seqData[k]
tempLine = editLine[0:20]
newLine = editLine.replace(editLine, tempLine)
newData = newData + sLine + newLine
if len(seqData[k]) > 20:
newData += '\n'
i=i+1
j=j+2
k=k+2
run=run+1
print(run)
seqFile.close()
new = open("new_temp100.txt", "w")
sys.stdout = new
print(newData)
newData=“”
i=0
运行=0
j=0
k=1
seqFile=open('temp100.txt','r')
seqData=seqFile.readlines()
而i<14371315:
sLine=seqData[j]
editLine=seqData[k]
模板行=编辑行[0:20]
换行符=编辑行。替换(编辑行,模板行)
newData=newData+sLine+newLine
如果len(seqData[k])>20:
newData+='\n'
i=i+1
j=j+2
k=k+2
运行=运行+1
打印(运行)
seqFile.close()
新建=打开(“new_temp100.txt”,“w”)
sys.stdout=new
打印(新数据)
我建议如下:
# if python 2.x
#from itertools import tee, izip
# if python 3
from itertols import tee
# http://docs.python.org/2/library/itertools.html#recipes
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
# if python 2.x
#return izip(a, b)
return zip(a, b)
new_data = []
with open('temp100.txt', 'r') as sqFile:
for sLine, edit_line in pairwise(seqFile):
# I think this is just new_line = tempLine
#tempLine = edit_line[:20]
#new_line = editLine.replace(editLine, tempLine)
new_data.append(sLine + editLine[:20])
if len(sLine) > 20:
new_data.append('\n')
with open("new_temp100.txt", "w") as new:
new.write(''.join(new_data))
如果直接将数据流传输到磁盘,您可能会做得更好
# if python 2.x
#from itertools import tee, izip
# if python 3
from itertols import tee
# http://docs.python.org/2/library/itertools.html#recipes
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
# if python 2.x
#return izip(a, b)
return zip(a, b)
new_data = []
with open('temp100.txt', 'r') as sqFile:
with open("new_temp100.txt", "w") as new:
for sLine, edit_line in pairwise(seqFile):
tmp_str = sLine + editLine[:20]
if len(sLine) > 20:
tmp_str = tmp_str + '/n'
new.write(tmp_str)
因此,您不必将文件的全部内容保存到内存中我建议这样做:
# if python 2.x
#from itertools import tee, izip
# if python 3
from itertols import tee
# http://docs.python.org/2/library/itertools.html#recipes
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
# if python 2.x
#return izip(a, b)
return zip(a, b)
new_data = []
with open('temp100.txt', 'r') as sqFile:
for sLine, edit_line in pairwise(seqFile):
# I think this is just new_line = tempLine
#tempLine = edit_line[:20]
#new_line = editLine.replace(editLine, tempLine)
new_data.append(sLine + editLine[:20])
if len(sLine) > 20:
new_data.append('\n')
with open("new_temp100.txt", "w") as new:
new.write(''.join(new_data))
如果直接将数据流传输到磁盘,您可能会做得更好
# if python 2.x
#from itertools import tee, izip
# if python 3
from itertols import tee
# http://docs.python.org/2/library/itertools.html#recipes
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
# if python 2.x
#return izip(a, b)
return zip(a, b)
new_data = []
with open('temp100.txt', 'r') as sqFile:
with open("new_temp100.txt", "w") as new:
for sLine, edit_line in pairwise(seqFile):
tmp_str = sLine + editLine[:20]
if len(sLine) > 20:
tmp_str = tmp_str + '/n'
new.write(tmp_str)
因此,您不必将文件的全部内容保存到内存中。相关:。您可能应该创建一个字符串列表,然后
“”。将(字符串)
连接起来,而不是循环和+=
连接。请参阅。您的缩进无法更正:。您可能应该创建一个字符串列表,然后''。将(字符串)
连接起来,而不是循环和+=
ing。请参阅。您的缩进无法更正。为什么打开一个文件进行读取?将内容添加到字符串,然后将其全部写入文件?通过嵌套两个打开的调用,您可以同时完成这两项工作——然后一次只编写每一个结果行。我想这会更快。@TimDiggins,因为OP就是这么做的(通过重置sys.stdout=new
)嗯,很有趣。但我得到了以下错误:从itertools导入izip导入错误:无法导入名称izip@TomAnonymous请参见编辑,您正在使用Python3,其中zip
的行为与python2中的izip
相同。我使用python2,忘记了这些事情。好的,我应用了编辑,它修复了错误问题。输出文本出现问题,但我可以自己处理。再次感谢!为什么要打开一个文件来读取并将内容添加到字符串中,然后将其全部写入文件?通过嵌套两个打开的调用,您可以同时完成这两项工作——然后一次只编写每一个结果行。我想这会更快。@TimDiggins,因为OP就是这么做的(通过重置sys.stdout=new
)嗯,很有趣。但我得到了以下错误:从itertools导入izip导入错误:无法导入名称izip@TomAnonymous请参见编辑,您正在使用Python3,其中zip
的行为与python2中的izip
相同。我使用python2,忘记了这些事情。好的,我应用了编辑,它修复了错误问题。输出文本出现问题,但我可以自己处理。再次感谢!