将 json 文件读取为 pandas 数据框?

新手上路,请多包涵

我正在使用 python 3.6 并尝试使用下面的代码将 json 文件(350 MB)下载为 pandas 数据框。但是,我收到以下错误:

>  data_json_str = "[" + ",".join(data) + "]
> "TypeError: sequence item 0: expected str instance, bytes found
>
> ```

我该如何修复错误?

import pandas as pd

read the entire file into a python array

with open(‘C:/Users/Alberto/nutrients.json’, ‘rb’) as f: data = f.readlines()

remove the trailing “\n” from each line

data = map(lambda x: x.rstrip(), 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)

”`

原文由 Alberto Alvarez 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 531
2 个回答

如果您以二进制形式打开文件 ( 'rb' ),您将获得字节。怎么样:

 with open('C:/Users/Alberto/nutrients.json', 'rU') as f:

同样如 答案所述,您也可以直接使用 pandas,例如:

 df = pd.read_json('C:/Users/Alberto/nutrients.json', lines=True)

原文由 Stephen Rauch 发布,翻译遵循 CC BY-SA 4.0 许可协议

从您的代码来看,您似乎正在加载一个 JSON 文件,该文件在每一行上都有 JSON 数据。 read_json 支持 lines 像这样的数据参数:

 data_df = pd.read_json('C:/Users/Alberto/nutrients.json', lines=True)

笔记

删除 lines=True 如果你有一个 JSON 对象而不是每行上的单个 JSON 对象。

原文由 cs95 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题