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

Python json.decoder.JSONDecodeError:预期值:第1行第1列(字符0),python,json,Python,Json,我正在尝试导入一个使用json.dumps保存的文件,该文件包含tweet坐标: { "type": "Point", "coordinates": [ -4.62352292, 55.44787441 ] } 我的代码是: >>> import json >>> data = json.loads('/Users/JoshuaHawley/clean1.txt') 但每次我都会出错: jso

我正在尝试导入一个使用
json.dumps
保存的文件,该文件包含tweet坐标:

{
    "type": "Point", 
    "coordinates": [
        -4.62352292, 
        55.44787441
    ]
}
我的代码是:

>>> import json
>>> data = json.loads('/Users/JoshuaHawley/clean1.txt')  
但每次我都会出错:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我希望最终提取所有坐标并将它们分别保存到另一个文件中,这样它们就可以被映射,但这个看似简单的问题阻止了我这样做。我看过类似错误的答案,但似乎无法将其应用于此。任何帮助都将不胜感激,因为我对python比较陌生。

json.loads()
采用json编码的字符串,而不是文件名。您想改用
json.load()
(no
s
)并传入一个打开的文件对象:

with open('/Users/JoshuaHawley/clean1.txt') as jsonfile:
    data = json.load(jsonfile)
open()
命令生成一个文件对象,然后
json.load()
可以读取该文件对象,从而为您生成解码的Python对象。
with
语句确保文件在完成后再次关闭

另一种方法是自己读取数据,然后将其传递到
json.loads()

我有类似的错误:“期望值:第1行第1列(字符0)”

它帮助我添加“myfile.seek(0)”,将指针移动到0字符

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

请使用此功能

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

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

为什么要使用
readlines()
?是的,从文件中读取首先将读取位置放在文件的末尾,因此需要返回到起始位置。但是,如果您只打开文件并使用
json.load()
,您一开始就不会处于这种状态。取消EVAL函数并使用json.load对我来说是一种享受。可以说,谢谢你给这个Python新手的提示。到目前为止,我已经编写了3个Python脚本。