简体   繁体   English

将JSON文件转换为.csv

[英]Converting JSON files to .csv

I've found some data that someone is downloading into a JSON file (I think! - I'm a newb!). 我发现有人将某些数据下载到JSON文件中(我想!-我是新手!)。 The file contains data on nearly 600 football players. 该文件包含有关近600名足球运动员的数据。

Here you can find the file 在这里您可以找到文件

In the past, I have downloaded the json file and then used this code: 过去,我已经下载了json文件,然后使用了以下代码:

import csv
import json

json_data = open("file.json")
data = json.load(json_data)

f = csv.writer(open("fix_hists.csv","wb+"))

arr = []

for i in data:
    fh = data[i]["fixture_history"]
    array = fh["all"]
    for j in array:

        try:
            j.insert(0,str(data[i]["first_name"]))
        except:
            j.insert(0,'error')

        try:
            j.insert(1,data[i]["web_name"])
        except:
            j.insert(1,'error')

        try:
            f.writerow(j)
        except:
            f.writerow(['error','error'])

json_data.close()

Sadly, when I do this now in command prompt, i get the following error: 可悲的是,当我现在在命令提示符下执行此操作时,出现以下错误:

Traceback (most recent call last): File"fix_hist.py", line 12 (module) fh = data[i]["fixture_history"] TypeError: list indices must be integers, not str 追溯(最近一次调用):文件“ fix_hist.py”,第12行(模块)fh = data [i] [“ fixture_history”] TypeError:列表索引必须是整数,而不是str

Can this be fixed or is there another way I can grab some of the data and convert it to .csv? 是否可以解决此问题,或者还有其他方法可以获取一些数据并将其转换为.csv吗? Specifically the 'Fixture History'? 特别是“固定记录”? and then 'First'Name', 'type_name' etc. 然后是“名字”,“类型名称”等。

Thanks in advance for any help :) 在此先感谢您的帮助:)

Try this tool: http://www.convertcsv.com/json-to-csv.htm 尝试使用此工具: http : //www.convertcsv.com/json-to-csv.htm

You will need to configure a few things, but should be easy enough. 您将需要配置一些东西,但是应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM