【问题标题】:How to convert this Json into CSV using python pandas?如何使用 python pandas 将此 Json 转换为 CSV?
【发布时间】:2018-09-16 01:46:46
【问题描述】:

试图将这个大的 json 转换成 csv。这里是示例 JSON

    [
    [{
            "User": "Phoebe",
            "text": "Oh my God, hes lost it. Hes totally lost it.",
            "sent": "non-neutral"
        },
        {
            "user": "Monica",
            "text": "What?",
            "sent": "surprise"
        }

    ],
    [{
            "user": "Joey",
            "text": "Hey Estelle, listen",
            "sent": "neutral"
        },
        {
            "user": "Estelle",
            "text": "Well! Well! Well! Joey Tribbiani! So you came back huh? They",
            "sent": "surprise"
        }
    ]
]


with open('/Users/dsg281/Downloads/EmotionLines/Friends/friends_dev.json') as data_file:    
        data = json.load(data_file) 

我正在尝试在 csv 中使用 "User""text""sent"

列获取输出

【问题讨论】:

标签: python python-3.x python-2.7 pandas


【解决方案1】:

我认为需要(将json更改为有效后):

file.json

[
  [
    {
      "user": "Phoebe",
      "text": "Oh my God, hes lost it. Hes totally lost it.",
      "sent": "non-neutral"
    },
    {
      "user": "Monica",
      "text": "What?",
      "sent": "surprise"
    }

   ],
   [{
      "user": "Joey",
      "text": "Hey Estelle, listen",
      "sent": "neutral"
    },
    {
      "user": "Estelle",
      "text": "Well! Well! Well! Joey Tribbiani! So you came back huh? They",
      "sent": "surprise"
    }
  ]     
]

import json

with open('file.json') as data_file:    
    data = json.load(data_file)  

df = pd.concat([pd.DataFrame(x) for x in data], ignore_index=False)
print (df)
          sent                                               text     user
0  non-neutral       Oh my God, hes lost it. Hes totally lost it.   Phoebe
1     surprise                                              What?   Monica
0      neutral                                Hey Estelle, listen     Joey
1     surprise  Well! Well! Well! Joey Tribbiani! So you came ...  Estelle

然后:

df.to_csv(file, index=False)

编辑:

如果想使用非 pandas 纯 python 解决方案,可以稍微修改一下this solution

import json, csv

with open('file.json') as data_file:    
    data = json.load(data_file)  

f = csv.writer(open("test.csv", "w+"))
f.writerow(["user", "sent", "text"])

for y in data:
    for x in y: #added for read nested lists
        f.writerow([x["user"], x["sent"], x["text"]])

【讨论】:

    【解决方案2】:

    如果我们提供一个空列表[]的起始值,我们可以将列表与sum连接在一起

    pd.DataFrame(sum(json.load(open('file.json')), [])).to_csv('file.csv', index=False)
    
    sent,text,user
    non-neutral,"Oh my God, hes lost it. Hes totally lost it.",Phoebe
    surprise,What?,Monica
    neutral,"Hey Estelle, listen",Joey
    surprise,Well! Well! Well! Joey Tribbiani! So you came back huh? They,Estelle
    

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2019-03-15
      • 2021-04-26
      • 2017-06-10
      • 2020-02-13
      • 2021-12-28
      • 2018-11-06
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多