我正在使用 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 许可协议
如果您以二进制形式打开文件 (
'rb'
),您将获得字节。怎么样:同样如 本 答案所述,您也可以直接使用 pandas,例如: