python脚本将数据输出为csv而不是JSON对象

时间:2016-01-30 15:57:02

标签: python json csv

我有一个像这样的数据集:

http://www.edsa-project.eu/adzuna/eyJhbGciOiJIUzI1NiJ9.eyJpIjoiMTA2MzEwMTkwIiwicyI6IkRvZnVlWGs1Unp5S2cyUlFOeWdXRmcifQ.v8u2XQwJUVflPipH2A_wg-HMrjYEl2BkL8EMOrRZ5m0,Back End Developer,n/a,Victoria,51.503378,-0.139134,United Kingdom,software development|sdlc|php|software design|compiler

我试图在每一行附加1,为此我使用以下python脚本:

import csv
reader = csv.reader(open("Y.csv", "rb"))
appendix = str('1')
for row in reader:
    row.append(appendix)
    print row

但我需要输出为.csv文档的形式,此过程以JSON-eque格式输出,如下所示:

['http://www.edsa-project.eu/adzuna/eyJhbGciOiJIUzI1NiJ9.eyJpIjoiMjY0OTM2Mzk3IiwicyI6IlR2dlB2cDZnU2RPajIxamF5R1BxbncifQ.xuWXv15319ifRV0LSShmSLZX0WxwH9kBW7e19osJZuM', '<strong>Analyst</strong>', 'Platinum Resources', 'Farringdon', '51.51985', '-0.11126', 'United Kingdom', 'leadership|finance|project management', '1']

如何才能使输出符合.csv

的样式

我尝试了一些受此启发的改编:

import csv

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(a)

但它也没有奏效,它最终在每个角色后面放了一个,

1 个答案:

答案 0 :(得分:-1)

DONE!回答我自己的问题!

with open('out.csv', 'r') as istr:
    with open('zhoutput.txt', 'w') as ostr:
        for line in istr:
            line = line.rstrip('\n') + ',1'
            print(line, file=ostr)