我正在使用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)
如果以二进制('rb')格式打开文件,则会得到字节。怎么样:
'rb'
with open('C:/Users/Alberto/nutrients.json', 'rU') as f: