我从互联网上获取货币然后尝试格式化它们以便每个货币都在新线上,希望有人可以帮助我
希望得到这样的输出:
{
" AED" :"阿拉伯联合酋长国迪拉姆",
" AFN" :"阿富汗阿富汗人",
...........
" ZWL" :"津巴布韦元"
}
我的代码如下:
import json
import urllib.request
f = urllib.request.urlopen('http://www.maths.manchester.ac.uk/~mbbssvs4/python/currencies.json')
charset = f.info().get_param('charset', 'utf8')
data = f.read()
decoded = json.loads(data.decode(charset))
print(decoded)
答案 0 :(得分:2)
尝试json.dumps
:
import json
j = json.loads("""{
"AED" : "united Arab Emirates Dirham",
"AFN" : "Afghan Afghani",
"ZWL" : "Zimbabwean Dollar"
}""")
print json.dumps(j, indent=4)
输出:
{
"AFN": "Afghan Afghani",
"ZWL": "Zimbabwean Dollar",
"AED": "united Arab Emirates Dirham"
}