# _*_ coding: utf-8 import xlrd,sys import pickle import json data=xlrd.open_workbook("d:/test/6.xlsx") ##读取工作表,方法可以按顺序索引找,也可以使用sheet_by_name(u"Sheet1") table = data.sheet_by_index(0) ###读取内容摘要表并存入nrarr nrarr={} table1 = data.sheet_by_index(1) for ro in range(2,table1.nrows): jj= table1.row(ro)[0].value #print ro if jj !="": nr=table1.row(ro)[1].value nrarr[jj]=nr #print nrarr[jj].encode('gbk','ignore') ''' #for k,v in nrarr.items(): # print k, v.encode('gbk', 'ignore') #print k.encode('gbk'),v.encode('gbk') ##获取工作表数量 #tables = data.nsheets ##获取整行,整列的值 (返回数组) #table.row_values(n) #table.col_values(n) ##单元格操作 #cell_A1 = table.cell(0,0).value #cell_C4 = table.cell(2,3).value ##行列索引 #table.row(0)[1].value #table.col(1)[0].value ''' ##获取总行数 nrows = table.nrows ##获取列数 ncols = table.ncols list=[] for rn in range(3, nrows): allarr={} jn = table.row(rn)[0].value allarr['xxx'] = jn allarr["xx"] = table.row(rn)[10].value allarr['xx'] =str(table.row(rn)[16].value) #allarr['xx'] = table.row(rn)[5].value allarr['xx'] = "" allarr['xx'] = table.row(rn)[7].value allarr['xx'] = table.row(rn)[8].value allarr['xx'] = u'中文' allarr['xx'] = table.row(rn)[4].value allarr['xx']=nrarr[jn] list.append(allarr) ##生成json并写入文件 js = json.dumps(list) output = open("d:/test/oo.json",'w') output.write(js) output.close() ###读json jf=json.load(open("d:/test/oo.json")) for ls in jf: for k,v in ls.items(): print k,v.encode('gbk','ignore') #print k,v