在python中解码JSON文本文件

时间:2014-02-11 00:12:33

标签: python json csv

我有一个文本文件,其中每一行是一个不同的JSON数组,具有相同的键集,但每行中的值不同。每行格式如下:

{"Key A":"Value A1","Key B":"Value B1","Key C":"Value C1, Value C2, Value C3"}

我想拉取一个键的值和另一个键的前4个值并导出到csv文件。

我希望输出看起来像这样:

Value A1      ["Value C1", "Value C2", "Value C3"]
Value A12      ["Value C12", "Value C22", "Value C32"]

到目前为止,我已将文件拆分为多行,如下所示:

import json
import csv

jsonmov = []
with open('step3_desired_output.txt') as step3:
    for line in step3:
        jsonmov.append(json.loads(line))


print jsonmov{u'Title',[u'Actors'[0:3]]}  #print each line from jsonmov's title and 4 actors

这给了我一个错误:

TypeError: list indices must be integers, not tuple

打印行的另一种语法:

print jsonmov(u'Title',u'Actors')

给出错误

TypeError: 'list' object is not callable:

有关如何以正确格式生成csv文件的任何想法?

2 个答案:

答案 0 :(得分:1)

import json
import csv

INPUT  = 'step3_desired_output.txt'
OUTPUT = 'my.csv'
MAXACTORS = 3

with open(OUTPUT, 'wb') as outf:
    outcsv = csv.writer(outf)
    with open(INPUT) as inf:
        for line in inf:
            mv = json.loads(line)
            title  = mv['Title']
            actors = mv['Actors'].split(', ', MAXACTORS)
            outcsv.writerow([title] + actors[:MAXACTORS])

答案 1 :(得分:1)

你的意思是:

import json
import csv

with open('/tmp/test.json') as f, open('/tmp/jout.csv', 'w') as fout:
    writer=csv.writer(fout)
    for line in f:
        jline=json.loads(line)
        print jline[u'Key A']+'\t['+jline[u'Key C']+']'
        # Value A1  [Value C1, Value C2, Value C3]
        # write to writer...

修改

也许:

import json

with open('/tmp/test.json') as f, open('/tmp/jout.csv', 'w') as fout:
    for line in f:
        data=[]
        jline=json.loads(line)
        print jline[u'Key A']+'\t['+', '.join('"{}"'.format(e.strip()) for e in jline[u'Key C'].split(','))+']'
        # Value A1  ["Value C1", "Value C2", "Value C3"]
        # add '\n' if you print to a file...