在一次导数据过程中,通过xlrd从Excel表格里获取内容,为什么表格中有的列获取不到,前0~6列的内容能够正常获取,到第7列时获取的内容实际为第10列的内容,中间的列为什么跳过去了?谢谢高手指点~~~急急!
#-*- coding:utf-8 -*- # 根据业务人员提供的科目代码excel生成交易相应的配置文件 import string import database as db import database.obj as obj import datetime from xlrd import xldate_as_tuple from xlrd import open_workbook def add_customer(file,sheet_name): wb = open_workbook(file) sheet = wb.sheet_by_name(sheet_name) fileHandle = open ( 'organization_customer.txt', 'a' ) for row in range(1,sheet.nrows): customer_no = transform_code_to_utf(sheet.cell(row,0).value) print customer_no id_type = transform_code_to_utf(sheet.cell(row,1).value) print id_type id_no = transform_code_to_utf(sheet.cell(row,2).value) print id_no registration_auth = transform_code_to_utf(sheet.cell(row,3).value) print registration_auth id_start_time = getdate(sheet.cell(row,4).value) print id_start_time id_end_time = getdate(sheet.cell(row,5).value) capital_currency = get_value(sheet.cell(row,23).value) china_sign = get_code(sheet.cell(row,24).value) customer_type = get_code(sheet.cell(row,31).value) public_enterprise_type = get_code(sheet.cell(row,32).value) organization_code = transform_code_to_utf(sheet.cell(row,6).value) print organization_code units_awarded = transform_code_to_utf(sheet.cell(row,7).value) print "XXXXX%sXXXXXX"%(units_awarded) sign_date = getdate(sheet.cell(row,8).value) print "IIIIIII%sIIIIIIIII"%(sign_date) end_date = getdate(sheet.cell(row,9).value) print "KKKKKKKKK%sKKKKKKK"%(end_date) customer_name = transform_code_to_utf(sheet.cell(row,10).value) print "QQQQQQ%sQQQQQQ"%(customer_name) english_name = transform_code_to_utf(sheet.cell(row,11).value) print "KKKKKK%sKKKKKKK"%(english_name) register_date = getdate(sheet.cell(row,12).value) print "LLLLL%sLLLLLLL"%(register_date) register_area_code = get_code(sheet.cell(row,13).value) print "TTTTTT%sTTTTT"%(register_area_code) credit_code_no = transform_code_to_utf(sheet.cell(row,14).value) print "PPPPPPP%sPPPP"%(credit_code_no) enterprise_property = transform_code_to_utf(sheet.cell(row,15).value) print "HHHHH%sHHHHHH"%(enterprise_property) corporate_enterprise = transform_code_to_utf(sheet.cell(row,16).value) print "UUUUUU%sUUUUUU"%(corporate_enterprise) if __name__=='__main__': if add_customer(u'数据文档/支行对公客户信息.xls',u'25银鹰')==False: exit(1)
尝试了一下,并没有出现你所说的情况
-----------------------------------------------
选用的xls内容如下:
测试程序如下:
from xlrd import open_workbook def add_customer(file,sheet_name): wb = open_workbook(file) sheet = wb.sheet_by_name(sheet_name) for row in range(0,sheet.nrows): for col in range(0,sheet.ncols): print "%4s" % sheet.cell(row, col).value, " ", print " " if __name__=='__main__': if add_customer(u'test.xls',u'Sheet1')==False: exit(1)
得到的结果如下:
----------------------------------------------------------------
希望对你有所帮助~~
非常感谢,原因找出来了,是项目组另外的人改动了Excel模板,他们把其中的列删除掉了,后来又还原了,而我不知道所以没有更新的缘故~