免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2661 | 回复: 1
打印 上一主题 下一主题

python json转dict遇ValueError: Expecting property name问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-11-28 17:43 |只看该作者 |倒序浏览
代码:
        with open(yamlOutPath) as fr:
                content = ''
                for line in fr.readlines():
                        content = content + line
               
                reRes = r'(ok):\s+\[([1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2})\]\s+=\>\s+({.*?})'
                resList = re.findall(reRes, str(content), flags = re.S + re.M)
                resListLen = len(resList)
               
                if resListLen > 0:
                        for iRes in range(resListLen):
                                print('status: %s' %resList[iRes][0])
                                print('ip: %s' %resList[iRes][1])
                                print('msg: %s' %resList[iRes][2])
                               
                                msg = resList[iRes][2].replace('\r\n', '')
                                msg = re.sub(r'\s+"', '"', msg)
                                print('msg type: %s' %type(msg))
                                print('msg pro: %s' %msg)
                                print(json.loads(msg))

                else:
                        print('No content match')
执行结果:
status: ok
ip: 165.254.88.23
msg: {
    "msg": [
        "The mmr started time",
        "The mmr running time is 2 days, 8 hours, 6 minutes, 44 seconds",
        "[Y] mmr /etc/cron.d/ssbguard be abled",
        "[Y] mmr /etc/cron.d/restartservice be abled",
        "The mmr version is Cloud MMR Release 3.6.109526.1009",
        "[N] mmr suspend",
        "[Y] mmr running and number of mmr process is 1",
        "[Y] mmr establish connections with port 8802",
        "[Y] mmr listening port 443",
        "[Y] mmr listening port 8801",
        "The mmr current user count is 0"
    ]
}
msg type: <type 'str'>
msg pro: {"msg": ["The mmr started time", "The mmr running time is 2 days, 8 hours, 6 minutes, 44 seconds", "[Y] mmr /etc/cron.d/ssbguard be abled", "[Y] mmr /etc/cron.d/restartservice be abled", "The mmr version is Cloud MMR Release 3.6.109526.1009", "[N] mmr suspend", "[Y] mmr running and number of mmr process is 1", "[Y] mmr establish connections with port 8802", "[Y] mmr listening port 443", "[Y] mmr listening port 8801", "The mmr current user count is 0"    ]}
Traceback (most recent call last):
  File "./call_yaml.py", line 89, in <module>
    call_yaml(yamlPath, inventoryPath, hosts)
  File "./call_yaml.py", line 66, in call_yaml
    print(json.loads(msg))
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

我的json字符串格式应该是没问题的,为什么json.loads()会报上述错误呢,麻烦大家帮帮我,谢谢大家

论坛徽章:
0
2 [报告]
发表于 2017-11-29 17:55 |只看该作者
找到问题所在了,因为字符串中存在控制字符,所以一直报ValueError: Expecting property name: line 1 column 2 (char 1),终究json字符串格式存在问题,删除字符串中的控制字符就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP