我是python的新手,根据我的论文工作,我正在尝试将JSON转换为csv。我能够以JSON格式下载数据但是当我使用字典将其写回时,它不会将JSON转换为CSV与每列
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np
import requests
from pprint import pprint
import csv
from time import sleep
s1='https://fantasy.premierleague.com/drf/element-summary/'
print s1
players = []
for player_link in range(1,450,1):
link = s1+""+str(player_link)
print link
r = requests.get(link)
print r
player =r.json()
players.append(player)
sleep(1)
with open('C:\Users\dell\Downloads\players_new2.csv', 'w') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f,player.keys())
w.writeheader()
for player in players:
w.writerow(player)
我上传了预期的输出(dec_15_expected.csv)和程序输出文件名“player_new_wrong_output.csv”
https://drive.google.com/drive/folders/0BwKYmRU_0K6tZUljd3Q0aG1LT0U?usp=sharing
如果有人能说出我做错了什么,这将是一个很大的帮助。
答案 0 :(得分:1)
使用pandas将JSON转换为CSV很简单。试试这个:
import pandas as pd
df=pd.read_json("input.json")
df.to_csv('output.csv')