一尘不染

将JSON文件读取为熊猫数据框?

json

我正在使用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)

阅读 263

收藏
2020-07-27

共1个答案

一尘不染

如果以二进制('rb')格式打开文件,则会得到字节。怎么样:

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