导入csv,操纵并导出到csv

时间:2016-11-29 07:10:59

标签: python python-3.x

我有一个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的{​​{1}}的最后一行。在我看来,好像代码只是覆盖了第一行。

output.csv看起来像这样:

input.csv

非常感谢您的帮助, 干杯, 弗朗兹

2 个答案:

答案 0 :(得分: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])

这种方式更有效且内存友好,特别是在输入文件非常大的情况下。

答案 1 :(得分:0)

尝试将 liste 赋值放在if循环中 -

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)