- 論壇徽章:
- 0
|
讀文件的方法:
from pyExcelerator import *
sheets = parse_xls("E:\work_python\wx\對(duì)應(yīng)400張卡卡號(hào).xls")
這樣 Execl 文檔中的數(shù)據(jù)就存入變量 sheets 中了.
分析讀出的數(shù)據(jù)
打印 sheets
>>> sheets
[(u'\u4e3b\u53f7\u526f\u53f7', {(0, 1): u'62172168', (0, 3): u'8985302080853097600---8985302080853097999', (0, 0): 1.0, (0, 2): u'15338172168'}), (u'Sheet2', {}), (u'Sheet3', {})]
發(fā)現(xiàn) sheets 為一個(gè) list. 該 list 中, 第一個(gè)元素為 "工作表1", 第二個(gè)為 "工作表2"
即, sheets = [工作表1, 工作表2, ...]
打印 sheets[0]
>>> sheets[0]
(u'\u4e3b\u53f7\u526f\u53f7', {(0, 1): u'62172168', (0, 3): u'8985302080853097600---8985302080853097999', (0, 0): 1.0, (0, 2): u'15338172168'})
sheets[0] 為一個(gè) tuple. tuple 的第一個(gè)元素為 "工作表"的表名, 第二個(gè)元素為 "工作表"的內(nèi)容.
即, sheets[0] = (工作表名, 工作表內(nèi)容)
分析工作表內(nèi)容.
打印 sheets[0][1]
>>> sheets[0][1]
{(0, 1): u'62172168', (0, 3): u'8985302080853097600---8985302080853097999', (0, 0): 1.0, (0, 2): u'15338172168'}
內(nèi)容存放在一個(gè) dict 中, 坐標(biāo)當(dāng)鍵、內(nèi)容為值。
因此 parse_xls 返回的結(jié)果結(jié)構(gòu)如下圖:
file:///C:/DOCUME%7E1/root/LOCALS%7E1/Temp/moz-screenshot-1.jpg
file:///C:/DOCUME%7E1/root/LOCALS%7E1/Temp/moz-screenshot-2.jpg
![]()
為了得到指定的元素, 比如表格1第1行第2列的元素:
sheets[0][1][(1,2)]
定義成函數(shù):
read_xls = lambda x, y, z: sheets[x][1][(y,z)]
函數(shù) read_xls 入?yún)? 工作表格序號(hào), 元素的行號(hào), 元素的列號(hào)
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/10686/showart_529096.html |
|