几个月前,突发奇想使用python来标记去过的省份和城市,就研究了一番,完成效果图如下:
1. Python源代码
python源代码如下:
#!/usr/bin/python
# encoding: utf-8
'''
@Author: EganWeng
@Contact: eganweng@163.com
@File: parse_excel.py
@Time: 8/7/2022 9:57 PM
'''
from pyecharts.charts import Map
from pyecharts import options as opts
from xpinyin import Pinyin
import xlrd
import random
# # 导入输出图片工具
# from pyecharts.render import make_snapshot
# # 使用snapshot-selenium 渲染图片
# from snapshot_selenium import snapshot
direct_city = ["上海", "北京", "天津", "重庆", "香港", "澳门"]
result = dict()
num_range = 1000
xls = xlrd.open_workbook(filename="china_egan.xls") # 打开文件。去国家统计网搞全国行政区 http://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2022/index.html
# xls = xlrd.open_workbook(filename="china.xls") # 打开文件
print("All sheets", xls.sheet_names())
sheet1 = xls.sheets()[0] # 获得第一张sheet,索引从0开始
sheet1_name = sheet1.name # 获得名称
sheet1_cols = sheet1.ncols # 获得列数
sheet1_nrows = sheet1.nrows # 获得行数
print('Sheet1 Name: %s\nSheet1 cols: %s\nSheet1 rows: %s' % (sheet1_name, sheet1_cols, sheet1_nrows))
# 循环获取行数据
for row in range(1, sheet1_nrows, 2): # 每两行一组进行遍历
rows1 = sheet1.row_values(row) # 获得第N行数据
if row + 1 < sheet1_nrows:
rows2 = sheet1.row_values(row+1) # 获得第N+1行数据
if sheet1.row(row)[1].value == "Y": # 判断第N行的第2个栏目是否为Y,表示该省份去过
province = sheet1.row(row)[0].value # 如果去过的话,那么把该省份的名字提取出来
# print(province)
list_city = []
for r1 in range(2, len(rows1)): # 提取去过该省份内的去过的城市
if rows2[r1] == "Y":
list_city.append((rows1[r1], random.randint(0, num_range)))
# print(rows1[r1])
result.update({province: list_city})
# print(result)
print("去过的省份或直辖市个数: {0}".format(len(result.keys())))
data = []
for key, item in result.items():
data.append((key, random.randint(0, num_range)))
# print(data)
# 生成中国地图
map=Map()
map.add("", data, maptype="china")
map.set_global_opts(
title_opts=opts.TitleOpts(title="去过的省份和直辖市:{0}个".format(len(result.keys())),subtitle="数据来源: Egan",pos_right="center",pos_top="5%"),
visualmap_opts=opts.VisualMapOpts(max_=num_range, is_piecewise=True),
)
map.render(path="china.html")
# 生成各个去过省的市地图
# for prov in result.keys():
# if len(result[prov]) != 0:
# map = Map()
# map.add("", list(result[prov]), maptype=prov)
# map.set_global_opts(
# # title_opts=opts.TitleOpts(title="去过的省份",subtitle="数据来源: Egan",pos_right="center",pos_top="5%"),
# visualmap_opts=opts.VisualMapOpts(max_=num_range),
# )
# name = Pinyin().get_pinyin(prov)
# map.render(name + ".html")
# 生成中国地图——包含各个城市
all_cities = list()
all_cities_name = list()
for prov in result.keys():
if len(result[prov]) != 0:
for c in result[prov]:
# all_cities.append((c[0].rstrip("市"), c[1])) 以前不需要填写"市"
all_cities.append((c[0], c[1]))
all_cities_name.append(c[0])
else:
all_cities.append((prov, random.randint(0, num_range)))
all_cities_name.append(prov)
print(all_cities)
map=Map()
map.add("", all_cities, maptype="china-cities", label_opts=opts.LabelOpts(is_show=False),)
map.set_global_opts(
title_opts=opts.TitleOpts(title="去过的城市:{0}个".format(len(all_cities)),subtitle="数据来源: Egan",pos_right="center",pos_top="5%"),
visualmap_opts=opts.VisualMapOpts(max_=num_range, is_piecewise=True),
)
map.render(path="china_all_cities.html")
print("去过的城市个数: {0}".format(len(all_cities)))
print(all_cities_name)
2. 使用方式
1. 创建1个xls格式的excel,注意excel的版本为Microsoft Excel 97-2003 Worksheet (.xls),而不是Microsoft Excel Worksheet (.xlsx)。excel的名字自己起,我的代码里默认为china_egan.xls,如果自己起的名字,那要把代码对应位置替换掉就行。
2. Excel里的内容需要用以下格式填充下。第一列是省份或直辖市,各占两行,第二列是对应是否去过,去过就写Y,没去过就写N。在省份的后面需要跟上城市,如果城市去过的话,就在下方标识上Y,没有的话就不需要填写任何东西。在这表里,我没有把省份对应的城市都写上,如果读者需要的话,可以自行补上。全国统计用区划分参考:2022年统计用区划代码和城乡划分代码
3. 执行上述python代码就可以当前运行目录下生成两份html格式的文件,用浏览器打开它们就可以看到图形了。python代码里有注释,读者可以参考。