0
我有一個JSON文件,我想從中提取一些數據並將其導出爲CSV。我有兩個循環獲取我想要的數據,但似乎沒有任何工作將其導出到CSV文件,請幫助,我是一個noob!使用Python/Pandas將JSON導出爲CSV
這裏是我的代碼:
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'])
這些都是爲得到我環路我想要的東西:
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'])
謝謝!這完美地工作,你已經做了我的一天! – Nigel78