json.decoder.JSONDecodeError:期望值:第1行第1列(字符0) [英] json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
问题描述
我正在尝试导入使用json.dumps
保存并包含tweet坐标的文件:
I am trying to import a file which was saved using json.dumps
and contains tweet coordinates:
{
"type": "Point",
"coordinates": [
-4.62352292,
55.44787441
]
}
我的代码是:
>>> 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)
我想最终提取所有坐标并将它们分别保存到另一个文件中,以便随后可以对其进行映射,但这看似简单的问题却使我无法这样做.我已经看过类似错误的答案,但似乎无法将其应用于此.任何帮助将不胜感激,因为我是python的新手.
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.
推荐答案
json.loads()
采用 JSON编码的字符串,而不是文件名.您想改用json.load()
(不使用s
)并传入打开的文件对象:
json.loads()
takes a JSON encoded string, not a filename. You want to use json.load()
(no s
) instead and pass in an open file object:
with open('/Users/JoshuaHawley/clean1.txt') as jsonfile:
data = json.load(jsonfile)
open()
命令生成一个文件对象,然后可以从中读取json.load()
,以为您生成已解码的Python对象. with
语句确保完成后再次关闭文件.
The open()
command produces a file object that json.load()
can then read from, to produce the decoded Python object for you. The with
statement ensures that the file is closed again when done.
另一种方法是自己读取数据,然后将其传递到json.loads()
.
The alternative is to read the data yourself and then pass it into json.loads()
.
这篇关于json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!