这是我的代码:
with open('step3_desired_output.txt') as f, open('jout.txt', 'w') as fout:
for line in f:
jline = json.dumps(line)
#jline2 = jline['Title']+'t['+jline['"'+'Actor'+'"']+']'+'n'
print jline2
我正在将一个 JSON 文件转储到 Python 中,然后我想组合一些值以形成字符串。 稍后我将使用 pydot 解析文件。
将 json 数据转储到 python 中的字符串(jline 变量)后,这是 ouptput:
"{"Title":"The Shawshank Redemption","Year":"1994","Rated":"R","Actors":"Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler","Plot":
像这样的行有多条。 我想将每行的标题和前 4 个 Actor 值输出到文本文件中,如下所示:
Title ["Actor","Actor","Actor","Actor"]
输出是类型错误:字符串索引必须是整数,而不是 str
更新最后,我换了一个方向,做了这件事:
file = open('step3_desired_output.txt','rU')
nfile = codecs.open('step4.txt','w','utf-8')
movie_actors = []
for line in file:
line = line.rstrip()
movie = json.loads(line)
l = []
title = movie['Title']
actors = movie['Actors']
tempactorslist = actors.split(',')
actorslist = []
for actor in tempactorslist:
actor = actor.strip()
actorslist.append(actor)
l.append(title)
l.append(actorslist)
row = l[0] + 't' + json.dumps(l[1]) + 'n'
nfile.writelines(row)
将jline = json.dumps(line)
更改为jline = json.loads(line)
。
编辑:您将具有以下结构:
{'Title': 'The Shawshank Redemption',
'Year': '1994',
'Rated': 'R',
'Actors': 'Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler'}
然后,您可以:
jline2 = {jline['Title']: jline['Actors'].split(', ')}
所以 jline2 将是以下内容:
{'The Shawshank Redemption': ['Tim Robbins',
'Morgan Freeman',
'Bob Gunton',
'William Sadler'] }
这种结构将很容易穿越。