2017-11-11 105 views
1

我有這個JSON文件,我試圖將其轉換爲CSV來收集數據。但是,我得到的結果遠不是正確的。我在使用python將JSON轉換爲CSV時遇到問題

到目前爲止,我有:

import csv 
import json 
infile = open("top40nl.json", "r") 
outfile = open("top40nl.csv", "w") 

writer = csv.writer(outfile) 

for row in json.loads(infile.read()): 
writer.writerow(row) 

使用這種類型的JSON數據(例如):

{ 
    "info": "SQLite Pro Result Set", 
    "data": [ 
     { 
      "top40_SK": "118899", 
      "song_title": "Sorry", 
      "artist": "Justin Bieber", 
      "year_released": "2015", 
      "year": "2016", 
      "week": "1", 
      "position": "1", 
      "prev_position": "1", 
      "weeks_in_top40_v1": "10", 
      "weeks_in_top40_v2": "10", 
      "highest_reached_position": "1", 
      "total_points": "775", 
      "top40url": "https://www.top40.nl/top40/2016/week-1" 
     }, 
     { 
      "top40_SK": "118900", 
      "song_title": "Love yourself", 
      "artist": "Justin Bieber", 
      "year_released": "2015", 
      "year": "2016", 
      "week": "1", 
      "position": "2", 
      "prev_position": "2", 
      "weeks_in_top40_v1": "6", 
      "weeks_in_top40_v2": "6", 
      "highest_reached_position": "1", 
      "total_points": "764", 
      "top40url": "https://www.top40.nl/top40/2016/week-1" 
     } 
    ] 
} 

以上所有的工作,到目前爲止,但是這是我的輸出: i,n,f,o d,a,t,a

任何想法如何解決這個問題?

+0

歡迎SO。請花時間閱讀[問]。你應該花一些時間在[教程](https://docs.python.org/3/tutorial/index.html)上練習,練習例子。它將爲您介紹Python所提供的工具,並且您甚至可以開始獲得解決問題的方法。 ...看起來像在迭代字符串的字符。 – wwii

+0

爲什麼你需要轉換爲CSV?爲什麼只需加載JSON數據並訪問? –

回答

1

我不明白你爲什麼要一個CSV文件,而不是JSON那張最簡單的一個,但這裏是如何從每個數據提取http://stardict.sourceforge.net/Dictionaries.php下載列出並將它們寫入CSV。爲了保持示例簡單,我只是將輸出寫入sys.stdout而不是寫入磁盤文件。

import json 
import csv 
import sys 

JSON = '''\ 
{ 
    "info": "SQLite Pro Result Set", 
    "data": [ 
     { 
      "top40_SK": "118899", 
      "song_title": "Sorry", 
      "artist": "Justin Bieber", 
      "year_released": "2015", 
      "year": "2016", 
      "week": "1", 
      "position": "1", 
      "prev_position": "1", 
      "weeks_in_top40_v1": "10", 
      "weeks_in_top40_v2": "10", 
      "highest_reached_position": "1", 
      "total_points": "775", 
      "top40url": "https://www.top40.nl/top40/2016/week-1" 
     }, 
     { 
      "top40_SK": "118900", 
      "song_title": "Love yourself", 
      "artist": "Justin Bieber", 
      "year_released": "2015", 
      "year": "2016", 
      "week": "1", 
      "position": "2", 
      "prev_position": "2", 
      "weeks_in_top40_v1": "6", 
      "weeks_in_top40_v2": "6", 
      "highest_reached_position": "1", 
      "total_points": "764", 
      "top40url": "https://www.top40.nl/top40/2016/week-1" 
     } 
    ] 
} 
''' 

data = json.loads(JSON) 

keys = data["data"][0].keys() 
writer = csv.DictWriter(sys.stdout, fieldnames=keys) 
writer.writerow(dict(zip(keys, keys))) 
for d in data["data"]: 
    writer.writerow(d) 

輸出

top40_SK,song_title,artist,year_released,year,week,position,prev_position,weeks_in_top40_v1,weeks_in_top40_v2,highest_reached_position,total_points,top40url 
118899,Sorry,Justin Bieber,2015,2016,1,1,1,10,10,1,775,https://www.top40.nl/top40/2016/week-1 
118900,Love yourself,Justin Bieber,2015,2016,1,2,2,6,6,1,764,https://www.top40.nl/top40/2016/week-1 
0

這裏使用熊貓庫

import pandas as pd 
import json 

with open('top40nl.json') as fi: 
    data = json.load(fi) 
    df = pd.DataFrame(data=data) 
    df.to_csv('top40nl.csv', index=False)