2017-06-06 55 views
0

我有一個db.sql文件,其中包含大量的url,如下所示。在python中,使用正則表達式搜索字符串並將其替換爲另一個

....<td class=\"column-1\"><a href=\"http://geni.us/4Lk5\" rel=nofollow\"><img src=\"http://www.toprateten.com/wp-content/uploads/2016/08/25460A-Panini-Press-Gourmet-Sandwich-Maker.jpg \" alt=\"25460A Panini Press Gourmet Sandwich Maker\" height=\"100\" width=\"100\"></a></td><td class=\"column-2\"><a href=\"http://geni.us/4Lk5\" rel=\"nofollow\">25460A Panini Press Gourmet Sandwich Maker</a></td><td class.... 

正如您所見,文件中有http://geni.us/4Lk5 \。

我有另一個product.csv文件,其中包含ID(如上面的4LK5)和Amazon產品URL,如下所示。

4Lk5 8738 8/16/2016 0:20 https://www.amazon.com/gp/product/B00IWOJRSM/ref=as_li_qf_sp_asin_il_tl?ie=UTF8 
Jx9Aj2 8738 8/22/2016 20:16 https://www.amazon.com/gp/product/B007EUSL5U/ref=as_li_qf_sp_asin_il_tl?ie=UTF8 
9sl2 8738 8/22/2016 20:18 https://www.amazon.com/gp/product/B00C3GQGVG/ref=as_li_qf_sp_asin_il_tl?ie=UTF8 

正如您所看到的,有4LK5與亞馬遜產品URL匹配。

我已經閱讀過csv文件,並且只用Python選取了ID和Amazon產品網址。

def openFile(filename, mode): 
    index = 0 
    result = [] 
    with open(filename, mode) as csvfile: 
     spamreader = csv.reader(csvfile, delimiter = ',', quotechar = '\n') 
     for row in spamreader: 
      result.append({ 
       "genu_id": row[0], 
       "amazon_url": row[3] 
      }); 
    return result 

我必須添加一些代碼來搜索合適的URL與genu_id在db.sql並與上面的代碼描述amazon_url更換。

請幫幫我。

+0

爲什麼你想用這個正則表達式,而不是用'lxml.html'或類似的方式解析單元格內容? –

+0

我是python的新手,所以我不太瞭解。我認爲我必須使用正則表達式來選擇'http://'+'geni.us/4Lk5'in ... ** - 1 \「>

回答