- 论坛徽章:
- 0
|
代码:
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()会报上述错误呢,麻烦大家帮帮我,谢谢大家
|
|