python三科成绩总和_Python使⽤xlrd模块先读取Excel数据
(如学⽣各科成绩。。。
使⽤python3读取处理excel表的数据内容如何对内容求平均值
先把数值读到⼀个列表⾥求和,然后再除以数量,就可以
例如:
python怎么读取excel的数据
最近由于经常要⽤Excel,需要根据Excel表格中的内容对⼀些apk进⾏处理,处理很⿇烦,于是决定写脚本来处理。⾸先贴出⽹上来的读写Excel的脚本。
1.读取Excel(需要安装xlrd):
123456789101112131415161718192021222324#-*- coding: utf8 -*-import xlrd fname = "reflect.xls"bk =
xlrd.open_workbook(fname)shxrange = range(bk.nsheets)try: sh = bk.sheet_by_name("Sheet1")except:
print "no sheet in %s named Sheet1" % fname#获取⾏数nrows = sh.nrows#获取列数ncols = sh.ncolsprint "nrows %d, ncols %d" % (nrows,ncols)#获取第⼀⾏第⼀列数据cell_value = sh.cell_value(1,1)#print cell_value row_list = []#获取各⾏数据for i in range(1,nrows): row_data = sh.row_values(i) row_list.append(row_data)
2.写⼊Excel(需安装pyExcelerator)
12345678from pyExcelerator import * w = Workbook() #创建⼀个⼯作簿ws = w.add_sheet('Hey, Hades') #创建⼀个⼯作表ws.write(0,0,'bit') #在1⾏1列写⼊bitws.write(0,1,'huang') #在1⾏2列写⼊huangws.write(1,0,'xuan') #在2⾏1列写⼊
xuanw.save('mini.xls') #保存
3.再举个⾃⼰写的读写Excel的例⼦
读取reflect.xls中的某些信息进⾏处理后写⼊mini.xls⽂件中。
1234567891011121314151617181920212223242526272829303132333435363738394041424344#-*- coding: utf8 -*-import xlrdfrom pyExcelerator import * w = Workbook()ws = w.add_sheet('Sheet1') fname = "reflect.xls"bk =
xlrd.open_workbook(fname)shxrange = range(bk.nsheets)try: sh = bk.sheet_by_name("Sheet1")except: print "no sheet in %s named Sheet1" % fname nrows = sh.nrowsncols = sh.ncolsprint "nrows %d, ncols %d" % (nrows,ncols) cell_value =
','.join(mydata) #写⼊数据到每⾏的第⼀列 ws.write(i,0,mydata) mydata = [] row_list.append(row_data[3])#print
row_list w.save('mini.xls')
4.现在我需要Excel⽂件中满⾜特定要求的apk的md5值来从服务器获取相应的apk样本,就需要这样做:
123456789101112131415161718192021222324252627282930313233#-*-coding:utf8-*-import xlrdimport osimport shutil fname = "./excelname.xls"bk = xlrd.open_workbook(fname)shxrange = ran
ge(bk.nsheets)try: #打开Sheet1⼯作表 sh = bk.sheet_by_name("Sheet1")except: print "no sheet in %s named Sheet1" % fname#获取⾏数nrows = sh.nrows#获取列数ncols = sh.ncols#print "nrows %d, ncols %d" % (nrows,ncols)#获取第⼀⾏第⼀列数据cell_value = sh.cell_value(1,1)#print
python怎么读入excelcell_value row_list = []#range(起始⾏,结束⾏)for i in range(1,nrows): row_data = sh.row_values(i) if row_data[6] ==
"HXB": filename = row_data[3] ".apk" #print "%s %s %s" %(i,row_data[3],filename) filepath = r"./1/" filename print "%s %s %s" %(i,row_data[3],filepath) if ists(filepath): py(filepath, r"./myapk/")
补充⼀个使⽤xlwt3进⾏Excel⽂件的写操作。
1234567891011121314151617181920212223242526import xlwt3 if __name__ == '__main__': datas = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]#⼆维数组 file_path = 'D:\\test.xlsx' wb = xlwt3.Workbook() sheet = wb.add_sheet('test')#sheet的名称为test #单元格的格式 style = 'pattern: pattern solid, fore_colour yellow; '#背景颜⾊为黄⾊ style = 'font: bold on; '#粗体字 style = 'align: horz centre, vert center; '#居中 header_style = xlwt3.easyxf(style) row_count =
len(datas) col_count = len(datas[0]) for row in range(0, row_count): col_count = len(datas[row]) for col in
range(0, col_count): if row == 0:#设置表头单元格的格式 sheet.write(row, col, datas[row][col],
header_style) else: sheet.write(row, col, datas[row][col]) wb.save(file_path)
输出的⽂件内容如下图:
注:以上代码在Python 3.x版本测试通过。
好了,python操作Excel就这么!些了,简单吧
python 提取excel指定单元格 保存为txt
import xlrd
tb=xlrd.open_workbook(r'd:/x/book123.xlsx')
sht=tb.sheets()[0]
for x in range(ws):
with open(r'd:/x/%s.txt' % ll_value(x,1),'w') as f:
f.ll_value(x,1) ',' ll_value(x,5) ',' ll_value(x,6) '\n')
python 如何读取excel⽂件 将每⼀⾏存为数组
A1
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论