파이썬 3에서 다음 코드를 실행하여 .txt
파일을 가져와 모든 두 번째 라인을 편집하고 편집 된 .txt
파일을 저장합니다. 그것은 작은 파일을 위해 잘 작동하지만, 내 파일은 ~ 2GB이며 너무 오래 걸립니다.성능 - 파이썬 텍스트 파일 편집 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)
관련 항목 : http://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python. – Aleph
아마도 루프 목록과'+ ='ing 대신에 문자열 목록을 만든 다음'' '.join (strings)'그들 목록을 만들어야합니다. http://stackoverflow.com/a/1967732/600110을 참조하십시오. –
http://stackoverflow.com/questions/17034895/python-editing-a-single-line-in-a-large-text-file – megawac