如何将JSON文件加载到Pandas dataframe中,具体错误

2024-05-01 21:59:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个任务,我正在尝试加载营养素.json文件放入熊猫数据框。我使用的是python3.5 我试过两种方法

  1. 数据源=pd.read_json文件("营养素.json“)

这将产生一个错误“ValueError:training data”

2。 #将整个文件读入python数组 打开('营养素.json','rb')作为f: 数据=f.readlines()

# remove the trailing "\n" from each line
data = map(lambda x: x.rstrip(), data)
data = map(str, data)
# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ','.join(data) + "]"

# now, load it into pandas
data_df = pd.read_json(data_json_str)

这就产生了一个错误

ValueError:应为对象或值

在营养素.json是我使用下面的说明提取的文件。它是一个335 Mb的文件。你能帮我一下吗。在

非常感谢你

  1. https://github.com/schirinos/nutrient-db.git查看GitHub中的nutrition db python实用程序

    1. 用python运行主程序营养数据库.py-e>;营养素.json将USDA数据转换为JSON格式。有关详细信息,请选中https://github.com/schirinos/nutrient-db。您可能需要通过pip install pymongo来安装python实用程序for MongoDB接口

Tags: and文件ofthe数据jsonreaddb
1条回答
网友
1楼 · 发布于 2024-05-01 21:59:57

把你的档案查一遍jsonlint.com网站查看文件是否正确格式化为json。在

您的第一次尝试书写正确:

df= pd.read_json('example_json.json')

相关问题 更多 >