我有一個csv文件,其中包含一些行和列。我需要從該表中選取一些特定值,然後將其增強,然後將它們導出到另一個csv文件。導入csv,操作並導出到csv
我的Python 3代碼如下所示:
import csv
from urllib.parse import quote
afn='https://r.refinedads.com/bs-mapping.php?v=a1&aid=7909&oid=1031&url='
zx='https://r.refinedads.com/bs-mapping.php?v=z1&aid=3814&oid=1031&zanpid=##zxClickID##&userid=##UserID##&url='
zxpn='https://r.refinedads.com/bs-mapping.php?v=z1&aid=3840&oid=1031&zanpid=##zxClickID##&userid=##UserID##&url='
la='https://r.refinedads.com/bs-mapping.php?aid=3843&oid=1031&c1=leadalliance&c2=XXXXX&c3=default&clid=YYYYY&url='
tv='https://r.refinedads.com/bs-mapping.php?aid=7639&oid=1031&c1=leadalliance&c2=XXXXX&c3=default&clid=YYYYY&url='
with open('input_csv.csv', 'r', newline='') as csvfile:
matrixreader = csv.DictReader(csvfile)
for row in matrixreader:
va = afn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzx = zx+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzxpn = zxpn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vla = la+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vtv = tv+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
liste = [[row['Name'], va, vzx, vzxpn, vla, vtv]]
print(liste)
with open('output.csv', 'w', newline='') as csvfile:
a = csv.writer(csvfile, delimiter=',')
a.writerows(liste)
作爲輸出我剛剛收到的input.csv
最後一排出口output.csv
。在我看來,代碼只是覆蓋第一行。
input.csv
看起來是這樣的:
*Name,Promocode,Link
BASE Plus + iPhone 7,ASDFNOWEDF,base.de/base-plus
BASE Pro + iPhone 7,JBONEDGASD,base.de/base-pro
BASE Light + iPhone 7,NAFODSFNTE,base.de/base-light
BASE Pur + iPhone 7,NAEWRIONF,base.de/base-pur*
非常感謝您在您的幫助, 乾杯, 弗朗茨
謝謝你們。 errikos是你的解決方案。 –