每个逗号后开始一个新行

时间:2015-04-06 10:22:07

标签: python json python-3.x newline

我从互联网上获取货币然后尝试格式化它们以便每个货币都在新线上,希望有人可以帮助我

希望得到这样的输出:

{

" 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)

1 个答案:

答案 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"
}

https://docs.python.org/2/library/json.html