Python3 Pygal Worldmap数据未显示

问题描述 投票:1回答:1

将json文件加载到列表中,转换为字典以加载到pygal worldmap中。当我打印字典时,数据看起来没问题(对我而言),但是地图打开(来自svg文件),但没有绘制数据。

没有发生回溯错误。

我没有运行最新版本的pygal。

字典的示例输出:

{地平线:9733784,仅:1437590,更低:92215683,OA:17394550,AGA:19061,读者:0}

代码如下:

import json

import pygal

# Load the data into a list.

filename = 'test.json'
with open(filename, 'rb') as f:
    sr_data = json.load(f)

# Print sr_data rows.

sr_exp = {}
for sr_dict in sr_data:
    country_code = sr_dict['CountryCode']
    gross = int(float(sr_dict['Exposed']))
    if country_code:
        sr_exp[country_code] = gross

# Create map.

wm = pygal.Worldmap()
wm.title = 'SR Data'
wm.add('',sr_exp)

wm.render_to_file('sr.svg')
python-3.x pygal
1个回答
0
投票

用作词典中键的国家/地区代码必须为小写。最简单的解决方法是更改​​线路

sr_exp[country_code] = gross

sr_exp[country_code.lower()] = gross
© www.soinside.com 2019 - 2024. All rights reserved.