Python读取xls文件
1、这里面有个问题在读取xlsx的时候有点问题,汇报异常,要把文件名字改成xls
import xlrd
import os
def getdata():
file_path = os.path.dirname(os.path.abspath(__file__))
base_path = os.path.join(file_path, '1.xls')
book = xlrd.open_workbook(base_path)
sheet1 = book.sheets()[0]
nrows = sheet1.nrows
print('表格总行数', nrows)
ncols = sheet1.ncols
print('表格总列数', ncols)
for i in range(nrows):
print("\n")
for j in range(ncols):
ctype = sheet1.cell(i, j).ctype # 表格的数据类型
cell = sheet1.cell_value(i, j)
if ctype == 0:#str类型
if(len(cell)== 0):
print(".",end='')#没有数据就用.来代替
elif ctype == 2 and cell % 1 == 0.0: # ctype为2且为浮点
cell = int(cell) # 浮点转成整型
print(cell , end="")
def main():
getdata()
pass
if __name__ =="__main__":
print("start")
main()