本文共 706 字,大约阅读时间需要 2 分钟。
自己随便写了一个表格如下
使用工具xlrd 如果没有下载 可以使用pip install xlrd 下载一个
大致需要一下几个步骤
1 打开excel
table = xlrd.open_workbook("/home/hly/hly/test.xls")
2 获取那一个excel 不如下面是获取第二个excel
sheet = table.sheet_by_index(1)
3 获取行数
rows = sheet.nrows
4 获取每行的内容
row_values
具体代码如下
# -*- coding: utf-8 -*-import xlrdimport sysreload(sys)sys.setdefaultencoding("utf-8")table = xlrd.open_workbook("/home/hly/hly/test.xls")# 通过索引获取,例如打开第二个sheet表格sheet = table.sheet_by_index(1)# 获取第二个excel的行数rows = sheet.nrowsprint rows# 根据行数遍历出整个表格content_list = []for i in range(rows): content_list.append(sheet.row_values(i))# 处理list中文乱码case_list = str(content_list).replace('u\'', '\'').decode("unicode-escape")print (case_list)
打印结果如下
这里只是遍历行数,当然方法不只是这一种。
转载地址:http://trfif.baihongyu.com/