python - 使用 pandas.read_json 时出现 ValueError

标签 python json pandas

我制作了一个 250MB 的 json 文件,它应该是这样的:

[ {"A":"uniquevalue0", "B":[1,2,3]}, 
  {"A":"uniquevalue1", "B":[1]}, 
  {"A":"uniquevalue2", "B":[1,2,3,4]} ]

其中“B”值可以是变量 len >= 1。This说我有有效的 JSON。

我打电话
df = pandas.read_json('ut1.json', orient = 'records', dtype={"A":str, "B":list})

Here是文档。读入 Pandas 数据帧时,我得到以下回溯:
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'

想不出哪里出了问题。 The python file that is throwing the error不是很有帮助。

最佳答案

我有同样的错误信息,我通过使用绝对路径解决了它。

import os
basePath = os.path.dirname(os.path.abspath(__file__))
df = pandas.read_json(basePath + '/ut1.json', orient = 'records', dtype={"A":str, "B":list})

这对我有用!

关于python - 使用 pandas.read_json 时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240982/

相关文章:

python - 用于 .append 函数的循环列表

Java 8 LocalDate Jackson 格式

python - 跨非空列连接字符串

python - 将秒数添加到 python 日期时间,不包括周末

python - 从 pandas 数据帧的所有列中的列表中删除 nan(pythonic 方式)

python - python 中的 Daubechies 正交小波

c++ - 使用 boost 读取复杂的 JSON 数组

jquery - LocalStorage,循环匹配的 ID,从 DOM 中删除

Pandas 绘制多个数据帧,一个数据帧产生一条平坦线

Python/Pandas - 计算具有特定索引的行数