I have a text file where each line is a different JSON array with the same set of keys but different values in each line. Each line is formatted like so:
{"Key A":"Value A1","Key B":"Value B1","Key C":"Value C1, Value C2, Value C3"}
I want to pull the values of one key and the first 4 values of another key and export to a csv file.
I want the output to look like this:
Value A1 ["Value C1", "Value C2", "Value C3"]
Value A12 ["Value C12", "Value C22", "Value C32"]
So far, I've split the file into multiple lines like this:
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
This gives me an error:
TypeError: list indices must be integers, not tuple
Another syntax for the print line:
print jsonmov(u'Title',u'Actors')
gives the error
TypeError: 'list' object is not callable:
Any ideas on how to produce the csv file in the right format?
json.loads(line)you end up with dictionaries. Attempting to calljson.loads(jsonmov)is redundant and causes errors. You already have a list of dictionaries.jsonmov[0]-> first dictionary in your list.jsonmov{...}supposed to mean (with the curly braces)?