使用pandas.read_json时发生ValueError [英] ValueError when using pandas.read_json
本文介绍了使用pandas.read_json时发生ValueError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个250MB的json文件,看起来应该像这样:
I made a 250MB json file that should look like this:
[ {"A":"uniquevalue0", "B":[1,2,3]},
{"A":"uniquevalue1", "B":[1]},
{"A":"uniquevalue2", "B":[1,2,3,4]} ]
其中"B"值可以是变量len> =1.这表示我有有效的JSON.
where the "B" value can be variable len >= 1. This says I have valid JSON.
我打电话
df = pandas.read_json('ut1.json', orient = 'records', dtype={"A":str, "B":list})
此处是该文档.读入pandas数据框时,我得到以下回溯:
Here is the documentation. When reading into a pandas dataframe, I get the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../pandas/io/json.py", line 198, in read_json
date_unit).parse()
File "/.../pandas/io/json.py", line 266, in parse
self._parse_no_numpy()
File "/.../pandas/io/json.py", line 496, in _parse_no_numpy
loads(json, precise_float=self.precise_float), dtype=None)
ValueError: Unexpected character found when decoding 'true'
想不到出了什么问题. 引发错误的python文件是没有那么大的帮助.
Can't think of what is going wrong. The python file that is throwing the error is not that helpful.
推荐答案
我有相同的错误消息,并且我使用绝对路径解决了.
I had the same error message, and I solved it by using an absolute path.
import os
basePath = os.path.dirname(os.path.abspath(__file__))
df = pandas.read_json(basePath + '/ut1.json', orient = 'records', dtype={"A":str, "B":list})
对我有用!
这篇关于使用pandas.read_json时发生ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文