#coding=utf-8
import xlrd
from xlwt import Workbook
gold = 5
silver = 3
copper = 1
def read_info_from_sheet(table):
nrows = table.nrows
info_dict = {}
for i in range(nrows):
row = table.row_values(i)
country = row[1]
goldcount = int(row[2])
silvercount = int(row[3])
coppercount = int(row[4])
info_dict[country]= goldcount*gold + silvercount*silver + coppercount*copper
return info_dict
def read_info_from_file(filename):
data = xlrd.open_workbook(filename)
country_set = set()
sheets = data.sheets()
info = {}
for sheet in sheets:
sheetinfo = read_info_from_sheet(sheet)
info[int(sheet.name)] = sheetinfo
return info
def write_info(filename,info):
score_dict = {}
year_lst = []
for year,infoitem in info.items():
year_lst.append(year)
for country,score in infoitem.items():
if not country in score_dict:
score_dict[country] = 0
score_dict[country] += score
score_dict= sorted(score_dict.iteritems(), key=lambda d:d[1],reverse=True)
w = Workbook()
ws = w.add_sheet(u'奥运奖牌')
ws.write(0,0,'country')
for i in range(0,len(year_lst)):
ws.write(0,i+1,str(year_lst[i]))
row_index = 1
for k,v in score_dict:
ws.write(row_index,0,k)
for i in range(0,len(year_lst)):
ws.write(row_index,i+1,info[year_lst[i]].get(k,0))
row_index += 1
w.save(filename)
def createinfo(filename):
info = read_info_from_file(filename)
write_info(u'/Users/kwsy/Desktop/历届奥运奖牌榜.xls',info)
if __name__ == '__main__':
createinfo(u'/Users/kwsy/Desktop/奥运奖牌.xlsx')
Kmeans算法为各国体育水平分类
需积分: 9 169 浏览量
2016-08-15
15:15:54
上传
评论 5
收藏 72KB ZIP 举报
酷python
- 粉丝: 226
- 资源: 9
最新资源
- 使用Dockerfile配置Springboot应用服务发布Docker镜像-源码
- 万维考试系统-C语言题库-含题目-程序设计题导出-113道.doc
- Alibaba SpringCloud集成Nacos、openFeign实现负载均衡-源码
- 用户研究系列之Persona介绍(用户画像)
- Image_1715110604427.jpg
- mYzh..trashed-1716951334-.jpg
- Alibaba SpringCloud集成nacos实现配置中心
- 那些好的不像话的心流体验
- 基于pytorch的卷积神经网络识别是否为奥特曼的项目python源码+文档说明+数据集(课程设计)
- 基于卷积神经网络的图像风格迁移python源码+文档说明+界面图片(课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈