替换文本文件中不正确的URL,并在Python中修复它们

2024-05-29 10:39:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到的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


Tags: 文件httpsurlwwwcardchallengesmco
1条回答
网友
1楼 · 发布于 2024-05-29 10:39:47
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)

我终于想到了这个。这有点笨拙,但它做得足够快

相关问题 更多 >

    热门问题