使用Python实现读取Excel表格中的数据
2018-01-11 13:47
417 查看
本案例的实现是通过使用第三方xlrd,关于xlrd的相关文章请自行百度,教程很多,不做详解。
1、如果使用的是Windows的cmd命令行,对于中文会出现编码问题,因为cmd支持的是GBK的编码,解决方案:将Excel文件重新导出时,更改编码为ut-8的编码方式
2、如果是从cmd命令行输入的编码问题,解决方案为:在cmd命令行下,输入chcp 65001,此时cmd的编码方式就变为utf-8
#-*- coding:utf-8 -*- import xdrlib ,sys import xlrd import setting import bz path = 'C:\\Users\\Desktop\\111.xls' def get_score(sheet,grade,sex,score): book = xlrd.open_workbook(path) sh = book.sheet_by_index(sheet-1) for j in xrange(2,sh.ncols): # 因为使用了utf-8编码,自然要进行解码 tmp_grade_name = sh.cell_value(1,j).decode('utf-8') tmp_sex = sh.cell_value(2,j) # 判断两个对象的值是否相等,如果判断对象是否相等,可以使用is关键来进行判断,建议使用==,cmp好像要废弃掉了 if tmp_grade_name == grade and cmp(tmp_sex,sex) == 0: for i in xrange(3,sh.nrows): tmp_score = sh.cell_value(i,j) if score <= float(tmp_score) and i>3 and i<21: return sh.cell_value(i,1) elif score <= float(tmp_score) and i == 3: return sh.cell_value(3,1) elif score >= float(tmp_score) and i >= 20: return sh.cell_value(20,1) if __name__ == '__main__': tmp_str = raw_input() tmp_array = tmp_str.split(' ') if tmp_array[0]=='1': tmp_score = get_score(int(tmp_array[0]),tmp_array[1].encode('utf-8'), tmp_array[2].encode('utf-8'),float(tmp_array[3])) print tmp_score elif tmp_array[0] == '2': tmp_score = get_score(int(tmp_array[0]),tmp_array[1].encode('utf-8'), tmp_array[2].encode('utf-8'),float(tmp_array[3])) print tmp_score elif tmp_array[0] == '3': tmp_score = get_score(int(tmp_array[0]),tmp_array[1].encode('utf-8'), tmp_array[2].encode('utf-8'),float(tmp_array[3])) print tmp_score elif tmp_array[0] == '4': tmp_score = get_score(int(tmp_array[0]),tmp_array[1].encode('utf-8'), tmp_array[2].encode('utf-8'),float(tmp_array[3])) print tmp_score else: print "Error!!!"
1、如果使用的是Windows的cmd命令行,对于中文会出现编码问题,因为cmd支持的是GBK的编码,解决方案:将Excel文件重新导出时,更改编码为ut-8的编码方式
2、如果是从cmd命令行输入的编码问题,解决方案为:在cmd命令行下,输入chcp 65001,此时cmd的编码方式就变为utf-8
相关文章推荐
- 使用Python3 xlrd pymysql 实现读取Excel数据读取以及mysql存储
- 在Ajax中使用Flash实现跨域数据读取的实现方法
- python+httplib实现HTTP POST提交数据,使用Basic Authentication(基本验证)做登陆验证
- 使用struts2标签,从后台读取数据时实现横向循环然后在自动换行循环
- 使用java读取Excel表格中的数据 .
- Delphi中使用python脚本读取Excel数据
- 从此例中终于实现了从外部xml文件读取数据使用Flare 生成有节点有连线的Graph
- Python读取PostgreSQL数据并使用基于物品的协同过滤算法计算推荐结果
- 使用python提取html文件中的特定数据的实现代码
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- Python使用shelve模块实现简单数据存储的方法
- 使用Python创建MySQL数据库实现字段动态增加以及动态的插入数据
- [转] Windows下使用Python读取Excel表格数据
- Windows下使用Python读取Excel表格数据
- 使用JDBC的CachedRowSet实现将数据源中的数据读取到内存中进行离线操作
- IBatis.net使用一--用IBatis.net实现数据读取
- Python实现随机读取文本N行数据
- 在Ajax中使用Flash实现跨域数据读取
- 使用Python CGI实现服务器展现数据的API接口
- 在Ajax中使用Flash实现跨域数据读取的实现方法