python 遍歷讀寫excel

2021-08-21 17:56:19 字數 1148 閱讀 2982

# -*- coding:utf8

import xdrlib, sys

import xlrd

from zhudb import *

from zhu_outlets_class import *

from zhu_telecom_class import *

from datetime import datetime

def open_excel(file=u"d:\zhu\\project\\ots\\100個國外**.xlsx"):

data = xlrd.open_workbook(file)

return data

# 根據索引獲取excel**中的資料 引數:file:excel檔案路徑 colnameindex:表頭列名所在行的所以 ,by_index:表的索引

def excel_table_byindex(file=u"d:\zhu\\project\\ots\\100個國外**.xlsx", colnameindex=0, by_index=0):

data = open_excel(file)

table = data.sheets()[by_index]

nrows = table.nrows # 行數

ncols = table.ncols # 列數

colnames = table.row_values(colnameindex) # 某一行資料

listd = {}

for rownum in range(1, nrows):

row = table.row_values(rownum)

if row:

for i in range(len(colnames)):

return listd

def main():

tables = excel_table_byindex()

for rowkey in tables:

print rowkey

row=tables[rowkey]

for onek in row:

print "\t",onek,row[onek]

if __name__ == "__main__":

main()

python讀寫excel檔案

開啟excel檔案讀取資料 data xlrd.open workbook jinrong.xlsx 獲取工作表 table data.sheet by name name tabel data.sheets 0 tabel data.sheet by index 0 獲取行數和列數 nrows t...

python讀寫excel檔案

coding utf 8 import xlrd import xlwt from datetime import date,datetime def read excel 開啟檔案 workbook xlrd.open workbook r f demo.xlsx 獲取所有sheet print ...

python之讀寫excel檔案

讀 import xlrd defopen excel file file.xls try data xlrd.open workbook file return data except exception,e print str e 根據索引獲取excel 中的資料 引數 file excel檔案...