我有个字符串如下:{\"module\":\"sink\", \"hostname\":\"datastream2\", \"tag\":\"urs_queryuser_mq\", \"category\":\"mq\", \"ruler\":\"MQ_55f85a369b22441e\", \"count\":425, \"totalDelay\":-1, \"totalProcessDelay\":2947890, \"time\":1430395550006}"
当我使用Python shell 时 :
ddb='{\"module\":\"sink\", \"hostname\":\"datastream2\", \"tag\":\"urs_yd_login\", \"category\":\"hdfs\", \"ruler\":\"HDFS_5aedbdf799494932\", \"count\":2, \"totalDelay\":-1, \"totalProcessDelay\":24115, \"time\":1430360048929}'
eval(ddb)
{'category': 'hdfs', 'count': 2, 'tag': 'urs_yd_login', 'totalDelay': -1, 'time': 1430360048929, 'totalProcessDelay': 24115, 'ruler': 'HDFS_5aedbdf799494932', 'hostname': 'datastream2', 'module': 'sink'}
此时能够正常转换成字典类型,但是在程序里边这样写就会抛出异常:
脚本如下格式:
print format_data[1]
ddddd = r'%s' % format_data[1].strip('"')
print ddddd
log_detail = eval(ddddd)
输出如下:
{\"module\":\"sink\", \"hostname\":\"datastream2\", \"tag\":\"urs_queryuser_mq\", \"category\":\"mq\", \"ruler\":\"MQ_55f85a369b22441e\", \"count\":425, \"totalDelay\":-1, \"totalProcessDelay\":2947890, \"time\":1430395550006}"
{\"module\":\"sink\", \"hostname\":\"datastream2\", \"tag\":\"urs_queryuser_mq\", \"category\":\"mq\", \"ruler\":\"MQ_55f85a369b22441e\", \"count\":425, \"totalDelay\":-1, \"totalProcessDelay\":2947890, \"time\":1430395550006}unexpected character after line continuation character (, line 1)
麻烦各位大侠,帮小弟指点指点。
5 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
脚本里你用了 ddddd = r'%s' % format_data[1].strip('"') 来赋值的有 r
shell里没这样干,所以脚本出错了
陪伴而非守候
TA贡献1757条经验 获得超8个赞
import json
json_str = {\"module\":\"sink\", \"hostname\":\"datastream2\", \"tag\":\"urs_queryuser_mq\", \"category\":\"mq\", \"ruler\":\"MQ_55f85a369b22441e\", \"count\":425, \"totalDelay\":-1, \"totalProcessDelay\":2947890, \"time\":1430395550006}
my_dic = json.loads(json_str)
添加回答
举报
0/150
提交
取消