我是編程新手,我正在運行此腳本來清理大文本文件(超過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)只在循環中寫clean_sentence(並且不要收集在「句子」中)b)收集所有內容並在循環後面寫上「句子」。我更喜歡a),因爲它的內存密集程度較低,但速度較慢。 –