我正在开发一个自定义的Freeradius Python模块。我在变量中的数据低于STRING类型。如何将其转换为字典。
[
["Acct-Input-Octets", "47762756"],
["NAS-Port-Type", "Ethernet"],
["Acct-Session-Id", "\"8172fde7\""],
["Acct-Output-Gigawords", "0"],
["Service-Type", "Framed-User"],
["Acct-Unique-Session-Id", "\"f55597d3cc05fe3a\""],
["Called-Station-Id", "\"myhome\""],
["Acct-Authentic", "RADIUS"],
["Acct-Status-Type", "Interim-Update"],
["Acct-Output-Packets", "698213"],
["NAS-IP-Address", "192.168.100.100"],
["NAS-Port-Id", "\"vlan555\""],
["Acct-Output-Octets", "379021490"],
["Acct-Session-Time", "33900"],
["Calling-Station-Id", "\"A0:CC:DD:9F:F2:09\""],
["Framed-Protocol", "PPP"],
["User-Name", "\"testing\""],
["NAS-Identifier", "\"testing_radius\""],
["Event-Timestamp", "\"Sep 6 2014 02:09:40 PKT\""],
["Acct-Input-Gigawords", "0"],
["Framed-IP-Address", "172.16.100.100"],
["Acct-Input-Packets", "622069"],
["NAS-Port", "467422"],
["Acct-Delay-Time", "0"]
]
所以,例如, 我想将下面的地图映射为一个关键的谷值
[" Acct-Input-Octets"," 7281436"]
所以它应该是
data = {" Acct-Input-Octets" :7281436}
有些情况下会有像
这样的转义报价[" NAS-Identifier"," \" testing_radius \""]
我正在使用Python 2.6附带的CentOS 6.4
答案 0 :(得分:4)
我使用json
module:
dict(json.loads(inputstring))
json.loads()
调用将字符串转换为Python列表。 dict()
获取该列表中的键值对并从中构建字典。
将数值转换为整数是一个额外的步骤,但它可以集成:
dict((k, int(v) if v.isdigit() else v) for k, v in json.loads(inputstring))
演示:
>>> import json
>>> inputstring = r'''[["Acct-Input-Octets", "47762756"], ["NAS-Port-Type", "Ethernet"], ["Acct-Session-Id", "\"8172fde7\""], ["Acct-Output-Gigawords", "0"], ["Service-Type", "Framed-User"], ["Acct-Unique-Session-Id", "\"f55597d3cc05fe3a\""], ["Called-Station-Id", "\"myhome\""], ["Acct-Authentic", "RADIUS"], ["Acct-Status-Type", "Interim-Update"], ["Acct-Output-Packets", "698213"], ["NAS-IP-Address", "192.168.100.100"], ["NAS-Port-Id", "\"vlan555\""], ["Acct-Output-Octets", "379021490"], ["Acct-Session-Time", "33900"], ["Calling-Station-Id", "\"A0:CC:DD:9F:F2:09\""], ["Framed-Protocol", "PPP"], ["User-Name", "\"testing\""], ["NAS-Identifier", "\"testing_radius\""], ["Event-Timestamp", "\"Sep 6 2014 02:09:40 PKT\""], ["Acct-Input-Gigawords", "0"], ["Framed-IP-Address", "172.16.100.100"], ["Acct-Input-Packets", "622069"], ["NAS-Port", "467422"], ["Acct-Delay-Time", "0"]]'''
>>> dict(json.loads(inputstring))
{u'NAS-IP-Address': u'192.168.100.100', u'Acct-Status-Type': u'Interim-Update', u'Acct-Input-Packets': u'622069', u'NAS-Identifier': u'"testing_radius"', u'Acct-Session-Id': u'"8172fde7"', u'Acct-Input-Octets': u'47762756', u'NAS-Port-Type': u'Ethernet', u'Acct-Session-Time': u'33900', u'Service-Type': u'Framed-User', u'Acct-Output-Gigawords': u'0', u'Acct-Output-Octets': u'379021490', u'Acct-Delay-Time': u'0', u'Framed-Protocol': u'PPP', u'Acct-Authentic': u'RADIUS', u'Acct-Input-Gigawords': u'0', u'Calling-Station-Id': u'"A0:CC:DD:9F:F2:09"', u'NAS-Port': u'467422', u'Acct-Unique-Session-Id': u'"f55597d3cc05fe3a"', u'Event-Timestamp': u'"Sep 6 2014 02:09:40 PKT"', u'User-Name': u'"testing"', u'NAS-Port-Id': u'"vlan555"', u'Acct-Output-Packets': u'698213', u'Called-Station-Id': u'"myhome"', u'Framed-IP-Address': u'172.16.100.100'}
>>> pprint(_)
{u'Acct-Authentic': u'RADIUS',
u'Acct-Delay-Time': u'0',
u'Acct-Input-Gigawords': u'0',
u'Acct-Input-Octets': u'47762756',
u'Acct-Input-Packets': u'622069',
u'Acct-Output-Gigawords': u'0',
u'Acct-Output-Octets': u'379021490',
u'Acct-Output-Packets': u'698213',
u'Acct-Session-Id': u'"8172fde7"',
u'Acct-Session-Time': u'33900',
u'Acct-Status-Type': u'Interim-Update',
u'Acct-Unique-Session-Id': u'"f55597d3cc05fe3a"',
u'Called-Station-Id': u'"myhome"',
u'Calling-Station-Id': u'"A0:CC:DD:9F:F2:09"',
u'Event-Timestamp': u'"Sep 6 2014 02:09:40 PKT"',
u'Framed-IP-Address': u'172.16.100.100',
u'Framed-Protocol': u'PPP',
u'NAS-IP-Address': u'192.168.100.100',
u'NAS-Identifier': u'"testing_radius"',
u'NAS-Port': u'467422',
u'NAS-Port-Id': u'"vlan555"',
u'NAS-Port-Type': u'Ethernet',
u'Service-Type': u'Framed-User',
u'User-Name': u'"testing"'}
>>> dict((k, int(v) if v.isdigit() else v) for k, v in json.loads(inputstring))
{u'NAS-IP-Address': u'192.168.100.100', u'Acct-Status-Type': u'Interim-Update', u'Acct-Input-Packets': 622069, u'NAS-Identifier': u'"testing_radius"', u'Acct-Session-Id': u'"8172fde7"', u'Acct-Input-Octets': 47762756, u'NAS-Port-Type': u'Ethernet', u'Acct-Session-Time': 33900, u'Service-Type': u'Framed-User', u'Acct-Output-Gigawords': 0, u'Acct-Output-Octets': 379021490, u'Acct-Delay-Time': 0, u'Framed-Protocol': u'PPP', u'Acct-Authentic': u'RADIUS', u'Acct-Input-Gigawords': 0, u'Calling-Station-Id': u'"A0:CC:DD:9F:F2:09"', u'NAS-Port': 467422, u'Acct-Unique-Session-Id': u'"f55597d3cc05fe3a"', u'Event-Timestamp': u'"Sep 6 2014 02:09:40 PKT"', u'User-Name': u'"testing"', u'NAS-Port-Id': u'"vlan555"', u'Acct-Output-Packets': 698213, u'Called-Station-Id': u'"myhome"', u'Framed-IP-Address': u'172.16.100.100'}
>>> pprint(_)
{u'Acct-Authentic': u'RADIUS',
u'Acct-Delay-Time': 0,
u'Acct-Input-Gigawords': 0,
u'Acct-Input-Octets': 47762756,
u'Acct-Input-Packets': 622069,
u'Acct-Output-Gigawords': 0,
u'Acct-Output-Octets': 379021490,
u'Acct-Output-Packets': 698213,
u'Acct-Session-Id': u'"8172fde7"',
u'Acct-Session-Time': 33900,
u'Acct-Status-Type': u'Interim-Update',
u'Acct-Unique-Session-Id': u'"f55597d3cc05fe3a"',
u'Called-Station-Id': u'"myhome"',
u'Calling-Station-Id': u'"A0:CC:DD:9F:F2:09"',
u'Event-Timestamp': u'"Sep 6 2014 02:09:40 PKT"',
u'Framed-IP-Address': u'172.16.100.100',
u'Framed-Protocol': u'PPP',
u'NAS-IP-Address': u'192.168.100.100',
u'NAS-Identifier': u'"testing_radius"',
u'NAS-Port': 467422,
u'NAS-Port-Id': u'"vlan555"',
u'NAS-Port-Type': u'Ethernet',
u'Service-Type': u'Framed-User',
u'User-Name': u'"testing"'}
答案 1 :(得分:2)
>>> dict(json.loads(r'''[["Acct-Input-Octets", "47762756"], ["NAS-Port-Type", "Ethernet"], ["Acct-Session-Id", "\"8172fde7\""], ["Acct-Output-Gigawords", "0"], ["Service-Type", "Framed-User"], ["Acct-Unique-Session-Id", "\"f55597d3cc05fe3a\""], ["Called-Station-Id", "\"myhome\""], ["Acct-Authentic", "RADIUS"], ["Acct-Status-Type", "Interim-Update"], ["Acct-Output-Packets", "698213"], ["NAS-IP-Address", "192.168.100.100"], ["NAS-Port-Id", "\"vlan555\""], ["Acct-Output-Octets", "379021490"], ["Acct-Session-Time", "33900"], ["Calling-Station-Id", "\"A0:CC:DD:9F:F2:09\""], ["Framed-Protocol", "PPP"], ["User-Name", "\"testing\""], ["NAS-Identifier", "\"testing_radius\""], ["Event-Timestamp", "\"Sep 6 2014 02:09:40 PKT\""], ["Acct-Input-Gigawords", "0"], ["Framed-IP-Address", "172.16.100.100"], ["Acct-Input-Packets", "622069"], ["NAS-Port", "467422"], ["Acct-Delay-Time", "0"]]'''))
{u'NAS-IP-Address': u'192.168.100.100', u'Acct-Status-Type': u'Interim-Update', u'Acct-Input-Packets': u'622069', u'NAS-Identifier': u'"testing_radius"', u'Acct-Session-Id': u'"8172fde7"', u'Acct-Input-Octets': u'47762756', u'NAS-Port-Type': u'Ethernet', u'Acct-Session-Time': u'33900', u'Service-Type': u'Framed-User', u'Acct-Output-Gigawords': u'0', u'Acct-Output-Octets': u'379021490', u'Acct-Delay-Time': u'0', u'Framed-Protocol': u'PPP', u'Acct-Authentic': u'RADIUS', u'Acct-Input-Gigawords': u'0', u'Calling-Station-Id': u'"A0:CC:DD:9F:F2:09"', u'NAS-Port': u'467422', u'Acct-Unique-Session-Id': u'"f55597d3cc05fe3a"', u'Event-Timestamp': u'"Sep 6 2014 02:09:40 PKT"', u'User-Name': u'"testing"', u'NAS-Port-Id': u'"vlan555"', u'Acct-Output-Packets': u'698213', u'Called-Station-Id': u'"myhome"', u'Framed-IP-Address': u'172.16.100.100'}