我目前正在嘗試從大型文本文件中刪除大多數行,并將所選信息重寫為另一個。我必須逐行閱讀原始文件,因為這些行出現的順序是相關的。到目前為止,我能想到的最好的方法是只拉相關的行,并使用類似以下內容的方法重寫它們:with open('input.txt', 'r') as input_file: with open('output.txt', 'w') as output_file: # We only have to loop through the large file once for line in input_file: # Looping through my data many times is OK as it only contains ~100 elements for stuff in data: # Search the line line_data = re.search(r"(match group a)|(match group b)", line) # Verify there is indeed a match to avoid raising an exception. # I found using try/except was negligibly slower here if line_data: if line_data.group(1): output_file.write('\n') elif line_data.group(2) == stuff: output_file.write('stuff') output_file.close() input_file.close()但是,使用?1Gb文件和?120,000條匹配的行,該程序仍需要花費約8個小時才能運行。我相信瓶頸可能涉及到正則表達式或輸出位,因為完成此腳本所花費的時間與行匹配數成線性比例。我曾嘗試先將輸出數據存儲在內存中,然后再將其寫入新的文本文件,但快速測試表明它以與之前寫入數據大致相同的速度存儲數據。如果有幫助,我有一個Ryzen 5 1500和8Gb的2133 Mhz RAM。但是,我的RAM使用率似乎從未達到上限。
添加回答
舉報
0/150
提交
取消