简体   繁体   中英

Export JSON to CSV using Python/Pandas

I have a JSON file which I want to extract some data from and export to CSV. I have two loops that get the data I want but nothing seems to work to export it to a CSV file, please help, I'm a noob!!

Here's my code:

   import csv
   import json 
   from pandas.io.json import json_normalize
   json_data = open('FuelCheckerV1.txt')
   fueldata = json.load(json_data)
   with open('out.csv') as csvfile:
          csv = csv.writer(
              csvfile,
              delimiter=',',
              quotechar='"',
              quoting=csv.QUOTE_MINIMAL
          )
          csv.writerow(['code', 'name', 'address', 'stationcode', 'fueltype', 'price', 'lastupdated'])
            for i in fueldata['stations']:
                csv.writerow(i['code'], i['name'], i['address'])
            for x in fueldata['prices']:
                csv.writerow(x['stationcode'], x['fueltype'], x['price'], x['lastupdated'])

These are the for loops that get me what I want:

for i in fueldata['stations']:
    print (i['code'], i['name'], i['address'])

for x in fueldata['prices']:
    print (x['stationcode'], x['fueltype'], x['price'], x['lastupdated'])

Assuming that the for loops above work as expected, you can try creating a list of records, using the pandas from_records method to create a dataframe, and then using the dataframe's to_csv method. For example:

import pandas as pd
import json

fueldata = json.load(open('FuelCheckerV1.txt'))

list_of_records = [
    (i['code'],
     i['name'],
     i['address'],
     x['stationcode'],
     x['fueltype'],
     x['price'],
     x['lastupdated']
    )
    for i, x in zip(fueldata['stations'], fueldata['prices'])
]

df = pd.DataFrame.from_records(
    list_of_records,
    columns = ['code', 'name', 'address', 'stationcode', 'fueltype', 
               'price', 'lastupdated']
)

df.to_csv('filename.csv')

There may be even more direct methods of creating a dataframe from your json, but this should work only knowing about the for loops in your example.

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