python - 在python中将带有子字段的Json转换为CSV

标签 python json csv tablib

我有一个包含示例 JSON 输出的文件,如下所示: jsonoutput.txt 文件:

[{"fruit": "orange", "id":1, "countries": ["Portugal"], "color": "Orange"}

{"fruit": "apple", "id":2, "countries": ["Portugal"], "color": "red"}]

我需要输出 csv(excel 文件):

fruit id countries color
orange 1 Portugal Orange
apple 2  Spain     red

现在,我越来越像 水果ID国家颜色 橙色 1 [u'葡萄牙'] 橙色 苹果 2 [u'西类牙'] 红

如何从列国家/地区中删除 [] 、 u 和 '' ?

print (json.dumps(fruits)) --给我 json 输出

这是我尝试将 json 转换为 xlsx 的方法:

data= tablib.Dataset(headers=('Fruit','id','Countries','Color'))
importfile = 'jsonoutput.txt'
data.json = open(importfile. 'r').read()
data_export = data.export('xlsx')
with open('output.xlsx','wb') as f:
    f.write(data_export)
    f.close()

最佳答案

你可以使用 pandas.io.json.json_normalize

import pandas as pd
from pandas.io.json import json_normalize

d = [
    {"fruit": "orange", "id":1, "countries": ["Portugal"], "color": "Orange"},
    {"fruit": "apple", "id":2, "countries": ["Portugal"], "color": "red"}
]

df = pd.concat([json_normalize(d[i]) for i in range(len(d))], ignore_index=True)
df['countries'] = df['countries'].str.join(' ')
<小时/>
    fruit   id  countries   color
0   orange  1   Portugal    Orange
1   apple   2   Portugal    red
<小时/>

要将其保存为 .xlsx 文件,请使用:

df.to_excel('filename.xlsx', index=False)
<小时/>

编辑:

json_normalize 是将半结构化 JSON 数据规范化为平面表的函数。

我现在实际上意识到我的代码可以简化为:

df = json_normalize(d) # no need for `pd.concat`

### Output:
#   fruit   id  countries   color
# 0 orange  1   ['Portugal']    Orange
# 1 apple   2   ['Portugal']    red

要从 countries 列中删除 [],我使用了 pandas.Series.str.joinpandas' 相当于 Python 的 str.join .

这是必需的,因为最初的countries列是包含元素的列表

df['countries'] = df['countries'].str.join(' ')
一旦您加入项目,

countries 列就不再是列表:

    fruit   id  countries   color
0   orange  1   Portugal    Orange
1   apple   2   Portugal    red

关于python - 在python中将带有子字段的Json转换为CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57528979/

相关文章:

python - 如何提高 scapy 读取大文件的性能

Python网络编程项目?

python - Pandas 条形图切断了x轴的两端

javascript - 如果没有找到任何项目,jQuery 执行 else 语句

java - 当使用\n 从字符串中读取 double 值时,扫描仪会出现 InputMismatchException — Java

python - 在 Python 中检查 CSV 的第一个单元格是否为空

javascript - 如果没有返回语句,Flask session 不会保留由 ajax 设置的值

Java:动态列表类型来转换 Json 数组

sql - PostgreSQL select * 其中列包含数组值

PHP - 基于另一个数组显示一个数组