【发布时间】:2017-03-23 17:00:56
【问题描述】:
我有一个任务,我正在尝试将 Nutrition.json 文件加载到 pandas 数据框中。我正在使用 python 3.5 我尝试了两种方法
data_df=pd.read_json("nutrients.json")
这给出了一个错误“ValueError: Trailing data”
2。 # 将整个文件读入python数组 使用 open('nutrients.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:预期的对象或值
nutrients.json 是我使用以下说明提取的文件。它是一个 335 Mb 的文件。请你在这里帮助我。
非常感谢
-
从https://github.com/schirinos/nutrient-db.git查看 GitHub 上的 nutrition-db python 实用程序
- 运行主程序,使用 python nutrientdb.py -e > Nutrition.json 将 USDA 数据转换为 JSON 格式。更多详情,请查看https://github.com/schirinos/nutrient-db。您可能需要通过 pip install pymongo 为 MongoDB 界面安装 python 实用程序
【问题讨论】:
-
你不是说熊猫数据框吗?
-
是的,这就是我的意思。我已经编辑了问题。这里有什么帮助吗?我无法附加 335MB 的文件...我可以在这里做些什么吗?
-
我只会用 tail 或其他东西检查文件的最后一行。你也可以用 json.load 加载它,看看会发生什么。你后面做的弦乐魔术真的是想多了。