【问题标题】:Python: Write list to individual columns in each rowPython:将列表写入每行中的各个列
【发布时间】:2011-02-11 05:13:48
【问题描述】:

我正在将数据写入 CSV,但当前输出如下: (最上面一行是标题)

 A    VC_s                                       VC_i    VT_s
 1    (['Not Reported'],['reported'],['click'])   
 2    (['Not Reported'],['reported'],['click'])

期望的输出如下:

 A    VC_s            VC_i         VT_s
 1    Not Reported    reported     click
 2    Not Reported    reported     click

到目前为止我得到的代码是:

ifile = csv.reader(open("input.csv",'rb'))
shutil.copy("input.csv","temp")
tempfile = csv.reader(open("temp","rb"))
ofile = csv.writer(open("RESULTS.csv","ab"))

for row in ifile:
    #do some table scraping stuff here
    VC_s = str(cells[1].find(text=True))
    VC_i = str(cells[2].find(text=True))
    VT_s = str(cells[4].find(text=True))

    entry = ([VC_s], [VC_i], [VT_s])
    rowAdd = tempfile.next()
    ofile.writerow(rowAdd + [entry])

我做错了什么,我该如何解决?这看起来很容易解决,但我很难过。

【问题讨论】:

    标签: python csv


    【解决方案1】:
    entry = [VC_s, VC_i, VT_s]
    rowAdd = tempfile.next()
    ofile.writerow(rowAdd + entry)
    

    【讨论】:

      【解决方案2】:

      你只想要entry = [VC_s, VC_i, VT_s]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-03
        • 1970-01-01
        • 2017-11-20
        • 1970-01-01
        • 2019-07-20
        • 1970-01-01
        • 2012-01-02
        • 1970-01-01
        相关资源
        最近更新 更多