python读取excel文件
来源:互联网 发布:用花洒自慰 知乎 编辑:程序博客网 时间:2024/04/30 22:44
一、读excel
import xlrddef open_excel(filename): # 打开文件xlrd.Book.encoding = "utf-8"try:data = xlrd.open_workbook(filename)return dataexcept Exception, e:print str(e)def excel_table_byindex(file = '1.xlsx', colnameindex=0, by_index=0):data = open_excel(file)table = data.sheets()[by_index] # 第几个sheetnrows = table.nrows # 行数ncols = table.ncols # 列数colnames = table.row_values(colnameindex) # 某一行数据list1 = [] # 新建一个支持表list2 = [] # 新建一个中立表list3 = [] # 新建一个反对表for rownum in range(1, nrows):row = table.row_values(rownum) # row是一行的数据if row:app={} #新建一个字典for i in range(len(colnames)):app[colnames[i]] = row[i]if app['attitude'] == 1:list1.append(app)elif app['attitude'] == 0list2. append(app)elif app['attitude'] == -1list3.append(app)return list1,list2,list3
title = table.col_values(index) # 读取某一列数据
二、写excel
import xlwtbook = xlwt.Workbook(encoding="utf-8", style_compression=0) # 创建一个workbooksheet = book.add_sheet('1', cell_overwrite_ok=True) # 添加一个sheetsheet.write(0,0,'title')sheet.write(0,1,'number')sheet.write(0,2,'news')sheet.write(0,3,'ren')sheet.write(0,4,'computer')sheet.write(row, 0, title1[m])sheet.write(row,1,number1[m])sheet.write(row,2,s)sheet.write(row,3,1)book.save(filename)
0 0
- Python excel文件读取
- python读取excel文件
- python读取excel文件
- python读取excel文件
- python读取excel文件
- Python读取Excel文件
- python读取excel文件
- python 读取excel文件
- Python读取Microsoft Excel文件
- python简单读取excel文件
- Python读取excel写入到RTF文件
- 读取excel文件并绘制图表[Python]
- 【python】 读取Excel文件并绘制图表
- Python读取excel文件(xlrd,xlwt)
- python读取excel文件(xrld模块)
- 【python】 读取Excel文件并绘制图表
- python使用xlwt读取文件写入Excel
- python读取excel文件—xlrd
- Android开发中Parcelable接口用法
- leetcode-java-268. Missing Number
- 3Sum Leetcode Python Java
- ImageLoader配置
- leetcode #150 in cpp
- python读取excel文件
- Android ListView、viewPager中ImageView图片错为混乱
- jQuery 事件 - resize() 方法
- const常量与宏定义区别
- 理解Java 虚拟机(一)
- Spring4.x官方参考文档中文版——第21章 Web MVC框架(16)
- mongodb去除重复的数据
- 虚拟机中实现Linux与Windows之间的文件传输
- Android画图paint的使用