python处理excel

上一篇 / 下一篇  2015-07-02 14:54:43 / 个人分类:python

一、系统性学习

对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址:

python操作Excel读写--使用xlrd

官方文档

Python 使用 Xlrd/xlwt 操作 Excel

用Python读写Excel文件

二、实战

帮朋友处理一个excel,幸好数据量不大,几万条数据。需求如下:

指定选取三列然后挑选出同时满足>=1或者同时<=-1的 将其所有数据存入新的csv表格中

程序如下:

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Date    : 2014-04-10 21:47:56# @Function: 指定选取三列然后挑选出同时满足>=1或者同时<=-1的 将其所有数据存入新的csv表格中# @Author  : BeginManimportosimportstringimportxlrdimportxlwtdefget_data():"""获取excel数据源"""file =r'C:\Users\Administrator\Desktop\pytool\xlrd\initial_log_data.xls'# 改成自己的路径filepath = raw_input(u'请将xls文件路径粘贴进去,如果程序里已经指定了文件则按Enter键继续:')
	is_valid =False# 验证文件try:
		filepath = [file, filepath][filepath !='']printfilepath# 判断给出的路径是不是xls格式ifos.path.isfile(filepath):
			filename = os.path.basename(filepath)iffilename.split('.')[1] =='xls':
				is_valid =Truedata =Noneifis_valid:
			data = xlrd.open_workbook(filepath)exceptException, e:printu'你操作错误:%s'%ereturnNonereturndatadefhandle_data():"""处理数据"""data = get_data()ifdata:
		col_format = ['B','C','D']# 指定的列inp = raw_input(u'请选择指定的三列,用逗号分隔,默认的是B,C,D(英文逗号,不区分大小写),如果选择默认则按Enter键继续:\n')try:
			inp = inp.split(',')
			col_format = [col_format,inp][len([iforiininpifiinstring.letters]) ==3]
			col_format = [i.upper()foriincol_format]# 转换成大写table = data.sheet_by_index(0)# 选取第一个工作nrows = table.nrows# 行数ncols = table.ncols# 列数str_upcase = [iforiinstring.uppercase]# 所有大写字母i_upcase = range(len(str_upcase))# 对应的数字ncols_dir = dict(zip(str_upcase,i_upcase))# 格式成字典col_index = [ncols_dir.get(i)foriincol_format]# 获取指定列所对应的索引# 选取的三列是否同时满足 >=1或者同时<=-1printu'正在检索中……'count =0result = []foriinxrange(nrows):
				cell_0 = table.cell(i,col_index[0]).value
				cell_1 = table.cell(i,col_index[1]).value
				cell_2 = table.cell(i,col_index[2]).valueif(cell_0>=1andcell_1>=1andcell_2>=1)or(cell_0<=-1andcell_1<=-1andcell_2<=-1):
					result.append(table.row_values(i))# 将符合要求的一行添加进去count +=1printu'该文件中共%s行,%s列,其中满足条件的共有%s条数据'%(nrows, ncols, count)printu'正在写入数据……'col_name = col_format[0]+col_format[1]+col_format[2]ifwrite_data(result, col_name):printu'写入成功!'exceptException, e:printu'你操作错误:%s'%ereturnNoneelse:printu'操作失败'returnNonedefwrite_data(data, name):"""写入数据,data为符合条件的数据列表,name表示指定的哪三个列,以此命名"""file = xlwt.Workbook()
	table = file.add_sheet(name,cell_overwrite_ok=True)
	l =0# 表示行forlineindata:
		c =0# 表示一行下的列数forcolinline:
			table.write(l,c,line[c])
			c +=1l +=1defatul_f =r'C:\Users\Administrator\Desktop\pytool\xlrd'# 默认路径f = raw_input(u'请选择保存文件的路径:按回车跳过:')
	f_name =r'\%s.xls'% name
	filepath = [defatul_f+f_name, f+f_name][f !='']
	file.save(filepath)returnTruedefmain():handle_data()if__name__ =='__main__':
	main()

运行结果如下:

源数据

处理后的数据


TAG: Excel Python python excel

 

评分:0

我来说两句

Open Toolbar