kegewe
kegewe

Reputation: 283

Dump a JSON file and then save to lists

This is my code:

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

I am dumping a JSON file into Python and I want to then combine some of the values to form strings. Later I'll be parsing the file using pydot.

After dumping the json data into a string in python (jline variable), this is the ouptput:

"{\"Title\":\"The Shawshank Redemption\",\"Year\":\"1994\",\"Rated\":\"R\",\"Actors\":\"Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler\",\"Plot\":

There are multiple lines like this. I want to ouput the Title and first 4 Actor values from each line to a text file, like so:

Title   ["Actor","Actor","Actor","Actor"]

Output is TypeError: string indices must be integers, not str

UPDATE In the end I went a different direction and did this:

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)

Upvotes: 0

Views: 1753

Answers (1)

licorna
licorna

Reputation: 5870

Change jline = json.dumps(line) to jline = json.loads(line).

Edit: You will have the following structure:

{'Title': 'The Shawshank Redemption',
 'Year': '1994',
 'Rated': 'R',
 'Actors': 'Tim Robbins, Morgan Freeman, Bob Gunton, William Sadler'}

Then you could:

jline2 = {jline['Title']: jline['Actors'].split(', ')}

So jline2 will be the following:

{'The Shawshank Redemption': ['Tim Robbins',
                              'Morgan Freeman',
                              'Bob Gunton',
                              'William Sadler'] }

And that structure will be easily traversable.

Upvotes: 2

Related Questions