2016-11-29 224 views
0

我有一個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* 

非常感謝您在您的幫助, 乾杯, 弗朗茨

+0

謝謝你們。 errikos是你的解決方案。 –

回答

0

嘗試把如果循環內的清單當然分配 -

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) 
    liste=[] # initialize here 
    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 = liste.append([[row['Name'], va, vzx, vzxpn, vla, vtv]]) # keep appending 
    print(liste) 

    with open('output.csv', 'w', newline='') as csvfile: 
     a = csv.writer(csvfile, delimiter=',') 
     a.writerows(liste) 
1

一個問題是,您嘗試在讀取循環之外更新liste變量。另一個問題是你重新分配給liste變量,而不是appending

但是,您不需要收集列表中的所有行,然後將它們寫入輸出。您可以通過在閱讀並形成時刻寫每行來做到這一點。

with open('input_csv.csv', 'r', newline='') as csvfile_in: 
    with open('output.csv', 'w', newline='') as csvfile_out: 
     matrixreader = csv.DictReader(csvfile_in) 
     a = csv.writer(csvfile_out, delimiter=',') 
     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']) 
      a.writerow([row['Name'], va, vzx, vzxpn, vla, vtv]) 

這樣更有效率和內存友好,特別是如果輸入文件非常大。