from country_code import get_country_codeimport jsonimport pygal.maps.worldfrom pygal.style import RotateStyle as RS, LightColorizedStyle as LCSfilename='gdp_json.json'with open(filename) as f: gdp_data=json.load(f)# 建立一個含有gdp的窗口gdp={}for gdp_m in gdp_data: if gdp_m['Year']=='1970':#這裏面應該加上單引號或雙引號 country_name=gdp_m['Country Name'] value=int(float(gdp_m['Value'])) code=get_country_code(country_name) if code: gdp[code]=value# 按gdp大小進行分類gdp_1,gdp_2,gdp_3={},{},{}for cc,dd in gdp.items(): if dd < 1e+11: gdp_1[cc]=dd elif dd < 1e+12: gdp_2[cc]=dd else: gdp_3[cc]=dd#將數據進行可視化print(len(gdp_1),len(gdp_2),len(gdp_3)) #這裏顯示出的三個字典的數量都爲0?這是爲何呢????wm_style=RS('#336699',base_style=LCS)wm=pygal.maps.world.World(style=wm_style)wm.title='GDP in 2015'wm.add('1',gdp_1)wm.add('2',gdp_2)wm.add('3',gdp_3)wm.render_to_file('gdp1.svg')