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

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

替換文本文件中的錯誤網址并在 Python 中修復它們

替換文本文件中的錯誤網址并在 Python 中修復它們

心有法竹 2023-06-27 16:28:00
我收到的 URL 已刪除前向睫毛,我基本上需要更正文本文件內的 url。文件中的 URL 如下所示:https:www.ebay.co.ukitmReds-Challenge-184-214-Holo-Shiny-Rare-Pokemon-Card-SM-Unbroken-Bonds-Rare124315281970?hash=item1cf1c4aa32%3Ag%3AXBAAAOSwJGRfSGI1&LH_BIN=1我需要將其更正為:https://www.ebay.co.uk/itm/Reds-Challenge-184-214-Holo-Shiny-Rare-Pokemon-Card-SM-Unbroken-Bonds-Rare/124315281970?hash=item1cf1c4aa32%3Ag%3AXBAAAOSwJGRfSGI1&LH_BIN=1所以基本上我需要一個正則表達式或其他方式來編輯文件中每個 URL 的正斜杠,并替換文件中損壞的 URL。
查看完整描述

1 回答

?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

while True:

    import time

    import re

    #input file

    fin = open("ebay2.csv", "rt")

    #output file to write the result to

    fout = open("out.txt", "wt")



    #for each line in the input file

    for line in fin:

        #read replace the string and write to output file

        fout.write(line.replace('https://www.ebay.co.uk/sch/', 'https://').replace('itm', '/itm/').replace('https:www.ebay','https://www.ebay'))


    with open('out.txt') as f:

      regex = r"\d{12}"

      subst = "/\\g<0>"

      for l in f:

          result = re.sub(regex, subst, l, 0, re.MULTILINE)

          if result:

              print(result)


    fin.close()

    fout.close()

    time.sleep(1)

我最終想出了這個。這有點笨拙,但完成工作的速度足夠快。


查看完整回答
反對 回復 2023-06-27
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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