Python 使用 pandas.read_json 时出现 ValueError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27240982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:33:13  来源:igfitidea点击:

ValueError when using pandas.read_json

pythonjsonpandas

提问by ehacinom

I made a 250MB json file that should look like this:

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

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

where the "B" value can be variable len >= 1. Thissays I have valid JSON.

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

I call

我打电话

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

Hereis 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'

Can't think of what is going wrong. The python file that is throwing the erroris not that helpful.

想不出哪里出了问题。抛出错误的 python 文件没有那么有用。

采纳答案by learn2day

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})

That worked for me!

那对我有用!

回答by user3614003

I was getting the "Value Error: Expected object or value" today while calling pandas.read_json('my_file.json'). I have ran this code with the same file earlier, so was very worried to see it is not working today. Later, I found for some reason, the json file was not there in the same dir. Then, I downloaded the file from git by right clicking the file link. That was a bad idea :(. I guess the json file was not encoded properly, so I kept getting the same error even when the json file was there in the same dir. At the end, I deleted the json file, cloned the original git repo to get the json file and put it in the same dir again. Then, this pandas.read_json did work. So, first of all please make sure the json file exists in the proper dir and then make sure, the file is not corrupted.

我今天在调用 pandas.read_json('my_file.json') 时收到“值错误:预期的对象或值”。我之前用同一个文件运行过这段代码,所以很担心今天它不起作用。后来我发现由于某种原因,json文件不在同一个目录中。然后,我通过右键单击文件链接从 git 下载文件。这是一个坏主意:(。我猜 json 文件没有正确编码,所以即使 json 文件在同一个目录中,我也一直收到同样的错误。最后,我删除了 json 文件,克隆了原始文件git repo 获取 json 文件并再次将其放入同一目录中。然后,这个 pandas.read_json 确实有效。因此,首先请确保 json 文件存在于正确的目录中,然后确保该文件不是损坏。

回答by imgg

After tried @learn2day's answer, I still cannot get a good result from there, but I do try the following code and everything works for me. (PS: I'm opening a JSON file where Chinese characters were UTF-8 characters appeared - Chinese characters)

在尝试了@learn2day 的回答后,我仍然无法从那里得到好的结果,但我确实尝试了以下代码,一切都对我有用。(PS:我打开了一个JSON文件,里面的汉字是UTF-8字符——汉字)

pandas.read_json(open("ut1.json", "r", encoding="utf8"))

The encoding="utf8"is the key part of this code.

encoding="utf8"此代码的关键部分。

回答by typhon04

In my case, the path was wrong.

就我而言,路径是错误的。

Make sure you check your current working directory, by placing this just before the pandas.read_json:

确保您检查您当前的工作目录,将其放在以下内容之前pandas.read_json

import os
print(os.getcwd())

回答by flome

Posting this because the answers above did not match my problem with this error: It just occurred for me when reading a very long string document I thought would be valid json format but contained nanas exported from python to string while it should be "null"for valid json. If the document was not created using a json package it might have faulty values indicated by this error message.

发布这个是因为上面的答案与我的问题与此错误不匹配:它只是在我阅读一个很长的字符串文档时发生,我认为这是有效的 json 格式,但包含nan从 python 导出到字符串,而它应该"null"用于有效的 json。如果该文档不是使用 json 包创建的,它可能具有此错误消息指示的错误值。

回答by Arun Krishnan

I had the same issue and then realized that the issue was while copying and pasting text from the browser to my file. It introduced carriage returns so that each line, for a given key was broken up into multiple rows. THAT was the issue. hope this helps someone!

我遇到了同样的问题,然后意识到问题出在将文本从浏览器复制并粘贴到我的文件时。它引入了回车,以便给定键的每一行都分成多行。这就是问题所在。希望这有助于某人!