2015-04-06 63 views
0

我從互聯網獲取的貨幣,然後試圖格式化所以每次貨幣是在新的一行,希望有人可以幫助我開始一個新行

希望得到的輸出是這樣的:

{

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

回答

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

+0

你非常非常感謝你Falko :) – PeteG 2015-04-06 10:36:51