0
我试图将text文件转换为python字典。如何将通过Web调用接收的文本文件转换为字典?
的基本格式是
"items_game"
{
"game_info"
{
"first_valid_class" "1"
"last_valid_class" "9"
"first_valid_item_slot" "0"
"last_valid_item_slot" "10"
"num_item_presets" "4"
}
"qualities"
{
"key" "value"
}
...
"community_market_item_remaps"
{
"Supply Crate"
{
"Supply Crate 2" "1"
"Supply Crate 3" "1"
}
"Decoder Ring"
{
"Winter Key" "1"
"Summer Key" "1"
"Naughty Winter Key 2011" "1"
"Nice Winter Key 2011" "1"
"Scorched Key" "1"
"Fall Key 2012" "1"
"Eerie Key" "1"
"Naughty Winter Key 2012" "1"
"Nice Winter Key 2012" "1"
}
}
}
该文件几乎是一本字典,但并不完全。有没有办法将它转换成字典,以便我可以通过键访问字典的每个级别?我想要这样做:
foreach key in dictName['items_game']['community_market_item_remaps']['Decoder Ring']:
# do something
谢谢你的帮助。
使用json.loads()避免来自外部源的数据上运行的eval()。我喜欢这种方法。 – Neil 2013-03-04 01:56:03