I am trying to import a file which was saved using json.dumps and contains tweet coordinates:

{
    "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)

我想最终提取所有坐标,并将它们分别保存到不同的文件中,这样它们就可以被映射,但这个看似简单的问题阻止了我这样做.我看过类似错误的答案,但似乎无法将其应用于此.任何帮助都将不胜感激,因为我对python比较陌生.

推荐答案

json.loads()接受JSON encoded string,而不是文件名.您希望改用json.load()(no 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. The with statement ensures that the file is closed again when done.

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

Json相关问答推荐

筛选JSON数组以使用Jolt仅保留具有最新日期/时间的条目

从json数组中删除特定元素

织女星-没有循环的动画条形图第二部分(实际上是织女星)

VBA json按特定属性名称提取所有数据

使用jq根据对象中键的值查找对象

jq - 将父键值提取为子元素旁边的逗号分隔值

使用 serde 和 csv crates 将嵌套的 json 对象序列化为 csv

将 JSON 字符串解析为 Kotlin Android 中的对象列表(MOSHI?)

通过sql查询读取嵌套Json

如何在 Django 的模板语言中获取 json 键和值?

使用 @ResponseBody 自定义 HttpMessageConverter 来做 Json 事情

在 Jersey 服务中使用 JSON 对象

如何在 Rails 模型中验证字符串是否为 json

无法将空值放入 JSON 对象

Microsoft.Net.Http 与 Microsoft.AspNet.WebApi.Client

JSON 和 BSON 哪个更轻量级?

设置对象不是 JSON 可序列化的

添加json文件注释

如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?

我如何反序列化以杰克逊为单位的时间戳?