json 如何修复错误:Pandas.errors.ParserError:标记数据时出错

2vuwiymt  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(125)

我试图把togheter几个JSON文件到一个CSV文件,并把这个文件在一个文件夹在我的电脑,但我得到了以下错误:
pandas.errors.ParserError:标记数据时出错。C错误:第8行中需要% 1个字段,看到% 2
到目前为止的代码:

from glob import glob
import pandas as pd
import os.path

path = '/mypath/*.json'
files = glob(path)

 data = pd.concat((pd.read_json(file, error_bad_lines=False)) for file in files)

 data.to_csv(os.path.join('/path where I want to put the csv file','yourfilename.csv'))

下面是JSON文件结构:

{
"data":[
  {
     "attachments":"3_1630878910203273218",
     "id":"1630878914296913926"
  },
  {
     "attachments":"4839577767895768",
     "id":"18945865497657868"
  }
     ],
"includes":{
  "media":[
     {
        "media_key":"3_1630878910203273218",
        "type":"photo",
        "url":"https://pbs.twimg.com/media/FqILumJXoAIOzy4.jpg"
     }
   ]
 },
"meta":{
  "newest_id":"1630878914296913926",
  "oldest_id":"1627974617309519873",
  "result_count":38
    }
   }

我所期望的是一个只包含JSON文件的“数据”部分的csv文件:
| 附件|身份证|
| --------------|--------------|
| 3_1630878910203273218|1630878914296913926|
| 4839577767895768|电话:18945865497657868|
我不知道我错在哪里

eqoofvh9

eqoofvh91#

IIUC,您可以使用json_normalize

path = '/mypath/*.json'
files = glob(path)
​
list_dfs = []
for file in files:
    with open(file, 'r') as f:
        list_dfs.append(pd.json_normalize(json.load(f)['data']))
​
data = pd.concat(list_dfs, ignore_index=True)
​
data.to_csv(os.path.join('/path where I want to put the csv file', 'yourfilename.csv'))

输出:

print(data)

             attachments                   id
0  3_1630878910203273218  1630878914296913926
1       4839577767895768    18945865497657868

相关问题