저는 프로그래밍에 익숙하지 않고 큰 텍스트 파일 (12000 줄 이상)을 정리하고 다른 .txt 파일에 쓰도록이 스크립트를 실행하고 있습니다. 문제는 파일을 작게 (대략 500 줄 정도) 실행하면 빠르게 실행되므로 결과적으로 파일 크기 때문에 시간이 걸리는 것입니다. 따라서 누군가이 코드를 효율적으로 만들 수 있도록 안내해 주시면 매우 감사하겠습니다.파일 프로세스에 쓰기가 더 효율적입니다.
input_file = open('bNEG.txt', 'rt', encoding='utf-8')
l_p = LanguageProcessing()
sentences=[]
for lines in input_file.readlines():
tokeniz = l_p.tokeniz(lines)
cleaned_url = l_p.clean_URL(tokeniz)
remove_words = l_p.remove_non_englishwords(cleaned_url)
stopwords_removed = l_p.remove_stopwords(remove_words)
cleaned_sentence=' '.join(str(s) for s in stopwords_removed)+"\n"
output_file = open('cNEG.txt', 'w', encoding='utf-8')
sentences.append(cleaned_sentence)
output_file.writelines(sentences)
input_file.close()
output_file.close()
편집 : 아래 답변으로 토론하도록하려면
input_file = open('chromehistory_log.txt', 'rt', encoding='utf-8')
output_file = open('dNEG.txt', 'w', encoding='utf-8')
l_p = LanguageProcessing()
#sentences=[]
for lines in input_file.readlines():
#print(lines)
tokeniz = l_p.tokeniz(lines)
cleaned_url = l_p.clean_URL(tokeniz)
remove_words = l_p.remove_non_englishwords(cleaned_url)
stopwords_removed = l_p.remove_stopwords(remove_words)
#print(stopwords_removed)
if stopwords_removed==[]:
continue
else:
cleaned_sentence=' '.join(str(s) for s in stopwords_removed)+"\n"
#sentences.append(cleaned_sentence)
output_file.writelines(cleaned_sentence)
input_file.close()
output_file.close()
모든 행에 대해 output_file을 엽니 다. 루프 위로 "output_file = open ('cNEG.txt', 'w', encoding = 'utf-8')"을 시도해보십시오. –
솔루션으로 회신 해 주셔서 감사합니다. @RalphErdt 그러나 취한 시간에는 큰 변화가 없었습니다. –
아 .. 뭔가를 감독했습니다. "문장"으로 모든 문자열을 수집하고 모든 루프를 반복적으로 작성합니다. -> a) 그냥 루프에 cleaned_sentence를 써 넣으십시오 (그리고 "문장"에서 수집하지 마십시오). b) 모든 것을 모으고 루프 뒤에 "문장"을 씁니다. 나는 그 메모리가 적기 때문에 메모리 사용량은 적지 만 느린 메모리를 선호하기 때문에). –