亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Python 3優化刪除線

使用Python 3優化刪除線

呼啦一陣風 2021-04-03 22:16:13
我目前正在嘗試從大型文本文件中刪除大多數行,并將所選信息重寫為另一個。我必須逐行閱讀原始文件,因為這些行出現的順序是相關的。到目前為止,我能想到的最好的方法是只拉相關的行,并使用類似以下內容的方法重寫它們: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使用率似乎從未達到上限。
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號