我正在尝试使用python程序执行nodejs脚本并获取返回输出。
b.js
data : {
"1":[],
"2":[],
"3":[],
"4":[],
"5":[],
"6":[],
"7":[],
"8":[],
"9":[],
"10":[],
"11":[{"id":"1","domain":"www.xxxxx.com","keywords":"白银交易平台","type":"Type1","time":["11","14","15","18"]}]
}
console.log(JSON.stringify(data))
a.py
if response.exitcode == 0:
print(response.stdout)
else:
sys.stderr.write(response.stderr)
b'{"1":[],"2":[],"3":[],"4":[],"5":[],"6":[],"7":[],"8":[],"9":[],"10":[],"11":[{"id":"1","domain":"www.xxxx.com","keywords":"\xe7\x99\xbd\xe9\x93\xb6\xe4\xba\xa4\xe6\x98\x93\xe5\xb9\xb3\xe5\x8f\xb0","type":"Type1","time":["11","14","15","18"]}]}\n'
从naked的官方文档中,response.stdout返回NakedObject。如何将返回的NakedObject转换为python字典?
答案 0 :(得分:0)
您似乎需要使用json module
。
<强>实施例强>
d = b'{"1":[],"2":[],"3":[],"4":[],"5":[],"6":[],"7":[],"8":[],"9":[],"10":[],"11":[{"id":"1","domain":"www.xxxx.com","keywords":"\xe7\x99\xbd\xe9\x93\xb6\xe4\xba\xa4\xe6\x98\x93\xe5\xb9\xb3\xe5\x8f\xb0","type":"Type1","time":["11","14","15","18"]}]}'
import json
print(json.loads(d))
<强>输出:强>
{u'11': [{u'keywords': u'\u767d\u94f6\u4ea4\u6613\u5e73\u53f0', u'domain': u'www.xxxx.com', u'type': u'Type1', u'id': u'1', u'time': [u'11', u'14', u'15', u'18']}], u'10': [], u'1': [], u'3': [], u'2': [], u'5': [], u'4': [], u'7': [], u'6': [], u'9': [], u'8': []}