Python:将列表写入每行中的各个列

发布于 2024-10-17 02:11:31 字数 945 浏览 1 评论 0原文

我正在将数据写入 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])

我做错了什么以及如何解决这个问题?这似乎是一个简单的解决办法,但我很困惑。

I'm writing data to a CSV but the current output is as follows:
(the top row is the headers)

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

The desired output is as follows:

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

The code I've got so far is:

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])

What am i doing wrong and how can I fix this? It would seem like an easy fix but I am stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-10-24 02:11:31
entry = [VC_s, VC_i, VT_s]
rowAdd = tempfile.next()
ofile.writerow(rowAdd + entry)
entry = [VC_s, VC_i, VT_s]
rowAdd = tempfile.next()
ofile.writerow(rowAdd + entry)
只是在用心讲痛 2024-10-24 02:11:31

您只需要 entry = [VC_s, VC_i, VT_s]

You just want entry = [VC_s, VC_i, VT_s]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文