简体   繁体   English

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

[英]json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I am trying to import a file which was saved using json.dumps and contains tweet coordinates:我正在尝试导入使用json.dumps保存并包含推文坐标的文件:

{
    "type": "Point", 
    "coordinates": [
        -4.62352292, 
        55.44787441
    ]
}

My code is:我的代码是:

>>> import json
>>> data = json.loads('/Users/JoshuaHawley/clean1.txt')  

But each time I get the error:但每次我得到错误:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I want to end up extracting all the coordinates and saving them separately to a different file so they can then be mapped, but this seemingly simple problem is stopping me from doing so.我想最终提取所有坐标并将它们分别保存到不同的文件中,以便它们可以被映射,但是这个看似简单的问题阻止了我这样做。 I have looked at answers to similar errors but don't seem to be able to apply it to this.我已经查看了类似错误的答案,但似乎无法将其应用于此。 Any help would be appreciated as I am relatively new to python.任何帮助将不胜感激,因为我对 python 比较陌生。

json.loads() takes a JSON encoded string , not a filename. json.loads()采用JSON 编码的字符串,而不是文件名。 You want to use json.load() (no s ) instead and pass in an open file object:您想改用json.load() (没有s )并传入一个打开的文件对象:

with open('/Users/JoshuaHawley/clean1.txt') as jsonfile:
    data = json.load(jsonfile)

The open() command produces a file object that json.load() can then read from, to produce the decoded Python object for you. open()命令生成一个文件对象,然后json.load()可以从中读取,从而为您生成解码的 Python 对象。 The with statement ensures that the file is closed again when done. with语句确保文件在完成后再次关闭。

The alternative is to read the data yourself and then pass it into json.loads() .另一种方法是自己读取数据,然后将其传递给json.loads()

I had similar error: "Expecting value: line 1 column 1 (char 0)"我有类似的错误:“期望值:第 1 行第 1 列(字符 0)”

It helped for me to add "myfile.seek(0)", move the pointer to the 0 character它有助于我添加“myfile.seek(0)”,将指针移动到 0 字符

with open(storage_path, 'r') as myfile:
if len(myfile.readlines()) != 0:
    myfile.seek(0)
    Bank_0 = json.load(myfile)

Please use this function请使用此功能

def read_json_file(filename):
    with open(filename, 'r') as f:
        cache = f.read()
        data = eval(cache)
    return data

another function另一个功能

def read_json_file(filename):
    data = []
    with open(filename, 'r') as f:
        data = [json.loads(_.replace('}]}"},', '}]}"}')) for _ in f.readlines()]
    return data

I got same type of error after reading in a json file creating from python.在读取从 python 创建的 json 文件后,我得到了相同类型的错误。

Same error occurred whether i read into a string and tried json.loads() or straight from file with json.load()无论我读入字符串并尝试 json.loads() 还是直接从文件中使用 json.load() 都会发生相同的错误

In my case, it turned out to be because I had written python booleans (False/True) straight out to the file.就我而言,结果证明是因为我已将 python 布尔值(假/真)直接写入文件。

Trying to read them back in again caused this error.尝试再次读回它们会导致此错误。 When i modified to valid json (true/false), json.load worked fine当我修改为有效的 json(真/假)时, json.load 工作正常

Didnt see any SO questions with this as a possible cause for this error so adding here for reference.没有看到任何与此有关的 SO 问题可能是导致此错误的原因,因此在此处添加以供参考。

import json导入json

file_path = "C:/Projects/Tryouts/books.json" file_path = "C:/Projects/Tryouts/books.json"

with open(file_path, 'r') as j: contents = json.loads(j.read()) print(contents)使用 open(file_path, 'r') as j: contents = json.loads(j.read()) print(contents)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 json.decoder.JSONDecodeError:期望值:第2行第1列(char 1)错误 - json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1) error 错误:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - ERROR: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 奇怪的“json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)” - WEIRD "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" ##json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)## - ##json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)## 什么是json.decoder.JSONDecodeError:期望值:第1行第1列(字符0) - What is json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 随机 json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Random json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 遇到:json.decoder.JSONDecodeError:预期值:第1行第1列(字符0) - Encounter: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 奇怪:json.decoder.JSONDecodeError:预期值:第1行第1列(字符0) - weird: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)python - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python json.decoder.JSONDecodeError:预期值:第2行第1列(字符2) - json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM