Python操作Excel_输出所有内容(包含中文)

python 2.7.5代码

 1 # coding=utf-8
 2 import sys
 3 import xlrd
 4 
 5 data=xlrd.open_workbook('D:\\menu.xls')
 6 
 7 table = data.sheet_by_name(u'肉食类')
 8 
 9 nrows_num = table.nrows
10  
11 ncols_num = table.ncols
12 
13 res=[]
14 
15 for nrows in range(nrows_num):
16     for ncols in range(ncols_num):
17         
18         cell_value = table.cell(nrows,ncols).value
19         
20         if cell_value=='':
21             cell_value='__'
22             res.append(cell_value)
23         elif isinstance(cell_value,unicode):
24                 cell_value=cell_value.encode('gb2312')
25                 res.append(cell_value)
26         elif isinstance(cell_value,float):
27                 cell_value = str(cell_value)
28                 cell_value = cell_value.decode('utf-8').encode('gb2312')
29                 res.append(cell_value)
30         elif isinstance(cell_value,int):
31                 cell_value = str(cell_value)
32                 cell_value = cell_value.decode('utf-8').encode('gb2312')
33                 res.append(cell_value)
34     res.append('|')
35     
36 res = ','.join(res)
37 res = res.split('|')
38 
39 for i in range(len(res)-1):
40     print '',i+1,'行数据:',res[i].strip(',')

 

测试结果

 

posted @ 2013-07-20 10:30  脚踏丷七星  阅读(6366)  评论(0编辑  收藏  举报