分配后Python对象类型发生了变化,这是一个错误吗?

时间:2014-12-17 13:52:23

标签: python json rabbitmq

AMQP消息队列接收boy变量的内容字符串。这是一个json字符串。它将被标准的json库

更改为python对象
body         = {"TestPlanName": "DemoPlan", "ReplyTo": "Reply", "Drivers": [1, 2, 3], "Cmd": "Start", "IsResetTool": true, "TestCycleId": 1}
logging.debug('Received message body: %s' % body)
msg          = json.loads(body)
for key, value in msg.items():
    logging.debug('key %s, type %s, value %s' % (key, type(value), value))
    logging.debug('key %s, type %s, value %s' % (key, type(value), msg[key]))

logging.debug('Parsed json object %s' % msg)

cmd          = msg[EXECUTOR.CMD],
drivers      = msg[EXECUTOR.DRIVERS], 
testCycleId  = msg[EXECUTOR.TEST_CYCLE_ID], 
testPlanName = msg[EXECUTOR.TEST_PLAN_NAME]
replyTo      = msg[EXECUTOR.REPLY_TO]

logging.debug('Parsed result type %s value %s' % (type(cmd          ), cmd          ))
logging.debug('Parsed result type %s value %s' % (type(drivers      ), drivers      ))
logging.debug('Parsed result type %s value %s' % (type(testCycleId  ), testCycleId  ))
logging.debug('Parsed result type %s value %s' % (type(dmType       ), dmType       ))

输出日志文件:

DEBUG msg:Received message body: {"TestPlanName": "DemoPlan", "ReplyTo": "Reply", "Drivers": [1, 2, 3], "Cmd": "Start", "DMType": "DM2", "IsResetTool": true, "TestCycleId": 1}
DEBUG msg:key TestPlanName, type <type 'unicode'>, value DemoPlan
DEBUG msg:key TestPlanName, type <type 'unicode'>, value DemoPlan
DEBUG msg:key ReplyTo, type <type 'unicode'>, value Reply
DEBUG msg:key ReplyTo, type <type 'unicode'>, value Reply
DEBUG msg:key Drivers, type <type 'list'>, value [1, 2, 3]
DEBUG msg:key Drivers, type <type 'list'>, value [1, 2, 3]
DEBUG msg:key Cmd, type <type 'unicode'>, value Start
DEBUG msg:key Cmd, type <type 'unicode'>, value Start
DEBUG msg:key IsResetTool, type <type 'bool'>, value True
DEBUG msg:key IsResetTool, type <type 'bool'>, value True
DEBUG msg:key TestCycleId, type <type 'int'>, value 1
DEBUG msg:key TestCycleId, type <type 'int'>, value 1
DEBUG msg:Parsed json object {u'TestPlanName': u'DemoPlan', u'ReplyTo': u'Reply', u'Drivers': [1, 2, 3], u'Cmd': u'Start', u'DMType': u'DM2', u'IsResetTool': True, u'TestCycleId': 1}
DEBUG msg:Before call ScriptDispatcher.ScriptDispatcher_CleanStopedExecutors args: () kwargs:{}     
DEBUG msg:After call ScriptDispatcher.ScriptDispatcher_CleanStopedExecutors  return: None
DEBUG msg:Parsed result type <type 'tuple'> value (u'Start',)
DEBUG msg:Parsed result type <type 'tuple'> value ([1, 2, 3],)
DEBUG msg:Parsed result type <type 'tuple'> value (1,)
DEBUG msg:Parsed result type <type 'unicode'> value DM2
DEBUG msg:Parsed result type <type 'unicode'> value DemoPlan
DEBUG msg:Parsed result type <type 'unicode'> value Reply

在分配msg字典的项目后,unicode字符串类型u'Start'更改为元组,列表[1,2,3]更改为([1,2,3],),数字1更改为(1 ,)!

3 个答案:

答案 0 :(得分:4)

你的作业中有尾随逗号,即cmd = msg[EXECUTOR.CMD],&lt; - 尾随逗号将使它成为一个元组删除它们,它将按预期行事。

答案 1 :(得分:3)

您的作业上有逗号逗号:

cmd          = msg[EXECUTOR.CMD],
drivers      = msg[EXECUTOR.DRIVERS], 
testCycleId  = msg[EXECUTOR.TEST_CYCLE_ID], 

所以真的是那些元组(偶然,我是假设)。

答案 2 :(得分:1)

当然这是一个错误,但是在你的代码中,而不是在Python中。

在Python中,元组由逗号定义。你的作业都以逗号结尾,因此它们是元组。

而不是

cmd          = msg[EXECUTOR.CMD],

DO

cmd          = msg[EXECUTOR.CMD]