python xlrd循环行列表数据 python读取excel文件,将每一行都保存为一个列表。每...

python\u5982\u4f55\u5faa\u73af\u8bfb\u53d6excel\u91cc\u9762\u6bcf\u4e2a\u5355\u5143\u683c\u7684\u5185\u5bb9\uff0c\u6211\u8981\u5c06\u6bcf\u4e00\u4e2a\u5355\u5143\u683c\u7684\u5185\u5bb9\u540c\u5176\u4ed6\u5355\u5143\u683c\u8fdb\u884c\u6bd4\u8f83

\u5148\u5b89\u88c5xlrd\u6a21\u5757
#-*- 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#\u83b7\u53d6\u884c\u6570nrows = sh.nrows#\u83b7\u53d6\u5217\u6570ncols = sh.ncolsprint "nrows %d, ncols %d" % (nrows,ncols)#\u83b7\u53d6\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\u6570\u636e cell_value = sh.cell_value(1,1)#print cell_value row_list = []#\u83b7\u53d6\u5404\u884c\u6570\u636efor i in range(1,nrows): row_data = sh.row_values(i) row_list.append(row_data)

python\u8bfb\u5199excel\u6587\u4ef6\u8981\u7528\u5230\u4e24\u4e2a\u5e93\uff1axlrd\u548cxlwt\uff0c\u9996\u5148\u4e0b\u8f7d\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\u3002

1\u3001#\u8bfb\u53d6Excel
import xlrd
data = xlrd.open_workbook(excelFile)
table = data.sheets()[0]
nrows = table.nrows #\u884c\u6570
ncols = table.ncols #\u5217\u6570
for i in xrange(0,nrows):rowValues= table.row_values(i) #\u67d0\u4e00\u884c\u6570\u636efor item in rowValues:print item
2\u3001\u5199Excel\u6587\u4ef6

'''\u5f80EXCEl\u5355\u5143\u683c\u5199\u5185\u5bb9\uff0c\u6bcf\u6b21\u5199\u4e00\u884csheet:\u9875\u7b7e\u540d\u79f0\uff1brow\uff1a\u884c\u5185\u5bb9\u5217\u8868\uff1browIndex\uff1a\u884c\u7d22\u5f15;
isBold:true:\u7c97\u5b57\u6bb5\uff0cfalse:\u666e\u901a\u5b57\u4f53'''
def WriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
#style = xlwt.easyxf('font: bold 0, color red;')#\u7ea2\u8272\u5b57\u4f53
#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # \u8bbe\u7f6eExcel\u5355\u5143\u683c\u7684\u80cc\u666f\u8272\u4e3a\u9ec4\u8272\uff0c\u5b57\u4f53\u4e3a\u7c97\u4f53
for svalue in rowValueList:
strValue = unicode(str(svalue),'utf-8')
if isBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i + 1

'''\u5199excel\u6587\u4ef6'''
def save_Excel(strFile):
excelFile = unicode(strFile, "utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)
headList = ['\u6807\u98981','\u6807\u98982','\u6807\u98983','\u6807\u98984','\u603b\u8ba1']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,True)
for i in xrange(1,11):
rowIndex = rowIndex + 1
valueList = []
for j in xrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)

style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')
\u5728\u8bbe\u7f6e\u4e0aExcel\u5355\u5143\u683c\u7684\u80cc\u666f\u8272\u65f6\uff0cfore_colour \u652f\u6301\u7684\u989c\u8272\u662f\u6709\u9650\u7684\uff0c\u4ec5\u652f\u6301\u4e00\u4e0b\u989c\u8272
aqua 0x31black 0x08blue 0x0Cblue_gray 0x36bright_green 0x0Bbrown 0x3Ccoral 0x1Dcyan_ega 0x0Fdark_blue 0x12dark_blue_ega 0x12dark_green 0x3Adark_green_ega 0x11dark_purple 0x1Cdark_red 0x10dark_red_ega 0x10dark_teal 0x38dark_yellow 0x13gold 0x33gray_ega 0x17gray25 0x16gray40 0x37gray50 0x17gray80 0x3Fgreen 0x11ice_blue 0x1Findigo 0x3Eivory 0x1Alavender 0x2Elight_blue 0x30light_green 0x2Alight_orange 0x34light_turquoise 0x29light_yellow 0x2Blime 0x32magenta_ega 0x0Eocean_blue 0x1Eolive_ega 0x13olive_green 0x3Borange 0x35pale_blue 0x2Cperiwinkle 0x18pink 0x0Eplum 0x3Dpurple_ega 0x14red 0x0Arose 0x2Dsea_green 0x39silver_ega 0x16sky_blue 0x28tan 0x2Fteal 0x15teal_ega 0x15turquoise 0x0Fviolet 0x14white 0x09yellow 0x0D"""\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u662f \u7528pyExcelerator


from pyExcelerator import *# excel \u7b2c\u4e00\u884c\u6570\u636eexcel_headDatas = [u'\u53d1\u5e03\u65f6\u95f4', u'\u6587\u7ae0\u6807\u9898', u'\u6587\u7ae0\u94fe\u63a5', u'\u6587\u7ae0\u7b80\u4ecb']articles =[{u'\u53d1\u5e03\u65f6\u95f4':u'2017\u5e745\u67089\u65e5',u'\u6587\u7ae0\u6807\u9898':u'Python\u9879\u76ee\u5b9e\u6218\u6559\u7a0b\uff1a\u56fd\u5185\u5c31\u80fd\u8bbf\u95ee\u7684google\u641c\u7d22\u5f15\u64ce',u'u'\u6587\u7ae0\u7b80\u4ecb':u'\u5927\u5bb6\u53ef\u4ee5\u7559\u8a00\u3001\u60f3\u4e86\u89e3python\u90a3\u4e2a\u65b9\u5411\u7684\u77e5\u8bc6\u3001\u4e0d\u7136\u6211\u4e5f\u4e0d\u77e5\u9053'},{u'\u53d1\u5e03\u65f6\u95f4':u'2017\u5e745\u67084\u65e5',u'\u6587\u7ae0\u6807\u9898':u'\u5bf9\u4e8e\u5b66\u4e60Django\u7684\u5efa\u8bae\u3001\u4f60\u77e5\u9053\u7684\u6709\u90a3\u4e9b',u'\u6587\u7ae0\u94fe\u63a5':',u'\u6587\u7ae0\u7b80\u4ecb':u'\u968f\u7740Django1.4\u7b2c\u4e8c\u4e2a\u5019\u9009\u7248\u7684\u53d1\u5e03\uff0c\u867d\u7136\u8fd8\u4e0d\u652f\u6301Python3\uff0c\u4f46Django\u56e2\u961f\u5df2\u7ecf\u5728\u7740\u624b\u8ba1\u5212\u4e2d\uff0c\u636e\u5b98\u65b9\u535a\u5ba2\u6240\u8bf4\uff0cDjango1.5\u5c06\u4f1a\u8bd5\u9a8c\u6027\u7684\u652f\u6301python3'}]# \u5b9a\u4e49excel\u64cd\u4f5c\u53e5\u67c4excle_Workbook = Workbook()excel_sheet_name = time.strftime('%Y-%m-%d')excel_sheet = excle_Workbook.add_sheet(excel_sheet_name)index = 0#\u6807\u9898for data in excel_headDatas:excel_sheet.write(0, index, data)index += 1index = 1#\u5185\u5bb9for article in articles:colIndex = 0 for item in excel_headDatas:excel_sheet.write(index, colIndex, article[item])colIndex += 1index += 1#\u4fdd\u5b58test.xlsx\u5230\u5f53\u524d\u7a0b\u5e8f\u76ee\u5f55excle_Workbook.save('test.xlsx')# db = mongoDB.mongoDbBase()# db.Get_information_stat()

我把一些基本的操作方法告诉你,你自然就会解决这个问题了。
1、导入模块
import xlrd
2、打开Excel文件读取数据
data = xlrd.open_workbook('excelFile.xls')
3、使用技巧
获取一个工作表

table = data.sheets()[0] #通过索引顺序获取

table = data.sheet_by_index(0) #通过索引顺序获取

table = data.sheet_by_name(u'Sheet1')#通过名称获取

获取整行和整列的值(数组)
  
table.row_values(i)

table.col_values(i)

获取行数和列数
  
nrows = table.nrows

ncols = table.ncols

循环行列表数据
for i in range(nrows ):
print table.row_values(i)

单元格
cell_A1 = table.cell(0,0).value

cell_C4 = table.cell(2,3).value

使用行列索引
cell_A1 = table.row(0)[0].value

cell_A2 = table.col(1)[0].value

简单的写入
row = 0

col = 0

# 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
ctype = 1 value = '单元格的值'

xf = 0 # 扩展的格式化

table.put_cell(row, col, ctype, value, xf)

table.cell(0,0) #单元格的值'

table.cell(0,0).value #单元格的值'

这个没做过,但是我觉得可以先从管道进行循环,找出里面多少日期。并返回日期所在的index,下面的管道的index应该是相关的,然后根据管道的index再进行处理。

提取日期可以用正则来做。
格式的话,一天构成一个处理循环。
管道总共14个。
一天的其中一个管道的话可以这样做:
temp = { x:'' for x in range(0,24)}

先构建一个字典出来。
然后根据index信息把字典填好。

主要思路就是这样了。具体代码可能需要你自己处理一下。

扩展阅读:python培训班学费一般多少 ... python编程入门自学 ... python初学编程必背 ... python代码自动生成器 ... python基础代码大全 ... python xlwt ... python xlsx ... python xlrd库 ... python编程for循环 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网