将 .json 文件转换为 .csv 文件

huangapple go评论71阅读模式
英文:

convert .json file to .csv

问题

df = pd.read_json('Data.json')['data'].apply(pd.Series)
df.to_csv('file.csv', index=False)
英文:

I have the following .json file:

data:	
    0:	
        area_code	"1A40"
        blacklist	0
        cost_center	"Payments "
        country	"Colombia"
        created_date	"Thu, 03 Jul 2014 12:57:02 GMT"
        .
        .
        .
    1:
        area_code	"1A42"
        blacklist	0
        cost_center	"Payments "
        country	"Colombia"
        created_date	"Thu, 03 Jul 2014 12:57:02 GMT"
        .
        .
        .
    2:{...}
    .
    .
    .
    1000:{...}

next url: "eyJzdGFydElu...="

I am using the following code to convert my .json file to csv :

import pandas as pd
df = pd.read_json('Data.json')['data']
df.to_csv('file.csv')

when I open the file from google sheet it brings me this way:

data
0 {'area_code': '1A40', 'blacklist': 0, 'cost_center': 'Payments', 'country': 'Colombia', 'created_date': 'Thu, 03 Jul 2014 12:57:02 GMT', 'deleted_date': None, 'department': 'Core Enablers', 'display_name': 'Oscar', 'division': 'IT', ...'}
1 {'area_code': '1A41', 'blacklist': 0, 'cost_center': 'Payments', 'country': 'Colombia', 'created_date': 'Thu, 03 Jul 2014 12:57:02 GMT', 'deleted_date': None, 'department': 'Core Enablers', 'display_name': 'Oscar', 'division': 'IT', ...'}
... {...}
999 {...}

What I'm needing is for the information to be listed in the sheet without repeating the headers for each cell

area_code blacklist cost_center country created_date ...
0 1A40 0 Payments Colombia Thu, 03 Jul 2014 12:57:02 GMT ...
1 1A42 0 Payments Colombia Thu, 03 Jul 2014 12:57:02 GMT ...
... ... .... ... ... .... ...
999 ... .... ... ... ... ...

Maybe you could tell me how I can list it like this?

答案1

得分: 1

你可以尝试

```py
import json
import pandas as pd

with open('data.json', 'r') as f_in:
    df = pd.DataFrame(json.load(f_in)['data'])

print(df)

#保存为CSV文件:
#df.to_csv('file.csv')

输出:

  area_code  blacklist cost_center   country                   created_date
0      1A40          0   Payments   Colombia  Thu, 03 Jul 2014 12:57:02 GMT
1      1A42          0   Payments   Colombia  Thu, 03 Jul 2014 12:57:02 GMT

data.json 文件的内容:

{
    "data": [
        {
            "area_code": "1A40",
            "blacklist": 0,
            "cost_center": "Payments ",
            "country": "Colombia",
            "created_date": "Thu, 03 Jul 2014 12:57:02 GMT"
        },
        {
            "area_code": "1A42",
            "blacklist": 0,
            "cost_center": "Payments ",
            "country": "Colombia",
            "created_date": "Thu, 03 Jul 2014 12:57:02 GMT"
        }
    ]
}
英文:

You can try:

import json
import pandas as pd

with open('data.json', 'r') as f_in:
    df = pd.DataFrame(json.load(f_in)['data'])

print(df)

#to save to CSV:
#df.to_csv('file.csv')

Prints:

  area_code  blacklist cost_center   country                   created_date
0      1A40          0   Payments   Colombia  Thu, 03 Jul 2014 12:57:02 GMT
1      1A42          0   Payments   Colombia  Thu, 03 Jul 2014 12:57:02 GMT

Contents of data.json file:

{
    "data": [
        {
            "area_code": "1A40",
            "blacklist": 0,
            "cost_center": "Payments ",
            "country": "Colombia",
            "created_date": "Thu, 03 Jul 2014 12:57:02 GMT"
        },
        {
            "area_code": "1A42",
            "blacklist": 0,
            "cost_center": "Payments ",
            "country": "Colombia",
            "created_date": "Thu, 03 Jul 2014 12:57:02 GMT"
        }
    ]
}

答案2

得分: 0

import pandas as pd
df = pd.read_json('Data.json')['data'].apply(pd.Series)
df.to_csv('file.csv')
英文:

You have to expand the dicts in the list. you can do this:

import pandas as pd
df = pd.read_json('Data.json')['data'].apply(pd.Series)
df.to_csv('file.csv')

huangapple
  • 本文由 发表于 2023年3月12日 06:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75709990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定