我会建议这样的事情:
# 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)
所以您不必将文件的全部内容保存到内存中