简体   繁体   中英

accessing a json list inside another list in python

I have a json file which I want to convert into csv,here are my codes

data=[]
with open('filename.json') as f:
    for line in f:
        data.append(json.loads(line))
f=csv.writer(open('filename.csv','wb+'))
for item in data:
    f.writerow([item['locations'][0]['time']])

The json contains three more lists inside and each list has the same attribute names. I can only access data in the first list; when I change f.writerow([item['locations'][0]['time']]) to f.writerow([item['locations'][-1]['time']]) it gives me access to the first and third lists (writing the first and third lists to csv) but it skips the second list. How can I access all the attributes in each of the lists, list 1-3?

Here is part of a single line of my json file

locations:[
{time : 1439319674334
longitude : 1.070336
local_time : "20:01:14:334 11 08 2015 +0100 GMT+01:00"
latitude : 51.2997804},
{time : 1439319694428
longitude : 1.0703332
local_time : "20:01:34:428 11 08 2015 +0100 GMT+01:00"
latitude : 51.2997889},

{time : 1439319714638
longitude : 1.0703123
local_time : "20:01:54:638 11 08 2015 +0100 GMT+01:00"
latitude : 51.2997794}

This single line can be converted into csv by following code:

locations:[
{time : 1439319674334
longitude : 1.070336
local_time : "20:01:14:334 11 08 2015 +0100 GMT+01:00"
latitude : 51.2997804},
{time : 1439319694428
longitude : 1.0703332
local_time : "20:01:34:428 11 08 2015 +0100 GMT+01:00"
latitude : 51.2997889},
{time : 1439319714638
longitude : 1.0703123
local_time : "20:01:54:638 11 08 2015 +0100 GMT+01:00"
latitude : 51.2997794} ]

file = open("filename.csv","a")

file.write("time, longitude, local_time, latitude\n")    

for location in locations:
    st = location['time'] + "," + location['longitude '] + "," + location['longitude'] + "," + location['latitude '] + "\n"
    file.write(st)

You'd make your life much easier using the csv library. Here try this

Try this:

In [75]: import csv
In [70]: j = {'locations':[
    ...: {'time' : 1439319674334,
    ...: 'longitude' : 1.070336,
    ...: 'local_time' : "20:01:14:334 11 08 2015 +0100 GMT+01:00",
    ...: 'latitude' : 51.2997804},
    ...: {'time' : 1439319694428,
    ...: 'longitude' : 1.0703332,
    ...: 'local_time' : "20:01:34:428 11 08 2015 +0100 GMT+01:00",
    ...: 'latitude' : 51.2997889},
    ...:
    ...: {'time' : 1439319714638,
    ...: 'longitude' : 1.0703123,
    ...: 'local_time' : "20:01:54:638 11 08 2015 +0100 GMT+01:00",
    ...: 'latitude' : 51.2997794}]}
    ...:
    ...:
In [74]: with open('output.csv', 'wb+') as f:
    ...:     writer = csv.DictWriter(f, fieldnames=j['locations'][0].keys())
    ...:     writer.writeheader()
    ...:     for item in j['locations']:
    ...:         writer.writerow(item)

You get output like this:

csvout(可能会损坏)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM