Python 突然不在 MAC OS X 上读取 json 文件

标签 python json macos

所以我在我的 macbook 上用 python 读取 json 文件时遇到了问题。这个问题很奇怪,因为直到几分钟前我才能够读取 json 文件。当我将 xcode 中的旧 json 文件更改为新的 json 文件时发生这种情况,见下文,有人可以帮我吗?

旧的 JSON 文件:

{
    "service 1” : {
        {
            "name" : "service 1"
        },
    }
    "service 2" : {
        {
            "name" : "service 2"
        }
    }
}

新的 JSON 文件:

{
    "services” : [
        {
            "name" : "service 1"
        },
        {
            "name" : "service 2"
        }
    ]
}

以下 python 脚本无法读取上述两个 json 文件:

import json

print "Running services: "

with open('service_list.json') as service_file:
    service_list = json.load(service_file)
    print(service_file);

运行脚本时出现如下错误:

Running services: Traceback (most recent call last): File "update_checker.py", line 6, in service_list = json.load(service_file) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 290, in load **kw) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Invalid control character at: line 2 column 22 (char 23)

谁能告诉我这是怎么回事? 提前致谢!

最佳答案

您有智能引号(或者如其他人所说,某种类型的基于 unicode 的引号)。你需要有常规的双引号。查看 services 的结尾引号:

{
    "services” : [
        {
            "name" : "service 1"
        },
        {
            "name" : "service 2"
        }
    ]
}

需要成为:

{
    "services" : [
        {
            "name" : "service 1"
        },
        {
            "name" : "service 2"
        }
    ]
}

关于Python 突然不在 MAC OS X 上读取 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46675283/

相关文章:

ios - 将值为 [NSNull null] 的字典转换为 JSON

python - 错误: "Traceback (most recent call last):"

python - 根据分布生成一组随机整数列表

python - 在 Python 中使用 mysql.connector 处理格式参数失败

php - 在 PHP 中,我从 mySQL 编码了一个 JSON 数组,如何使用它?解码?

c# - MVC JsonResult 有数据,但浏览器没有

macos - NSS slider 动画

c - OS X CommonCrypto 是否为 OpenSSL EVP_* 调用提供了兼容模式(就像 OpenSSL MD5 函数一样)?

python - 即使经过数小时的训练,神经网络(从头开始,没有 tensorflow )也会给出错误的答案

python - 使用 SWIG 将 numpy 数组元素(int)传递给 c++ int