我有這個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
任何想法如何解決這個問題?
歡迎SO。請花時間閱讀[問]。你應該花一些時間在[教程](https://docs.python.org/3/tutorial/index.html)上練習,練習例子。它將爲您介紹Python所提供的工具,並且您甚至可以開始獲得解決問題的方法。 ...看起來像在迭代字符串的字符。 – wwii
爲什麼你需要轉換爲CSV?爲什麼只需加載JSON數據並訪問? –