之前经常用python读取Excel内容,后来有一段时间不用了再用就给忘记了,觉得记录还是很有必要的
1. 首选安装xlrd
安装方法很简单,如果装了pip的话 pip install xlrd
2. python代码
实现的功能:将Excel的内容按照字典形式打印出来
结果:[{'user': 'hehe', 'psd': '1111111', 'result': '1111111'}, {'user': 'hehe', 'psd': '1111111', 'result': '1111111'}, {'user': 'hehe', 'psd': '1111111', 'result': '1111111'}, {'user': 'hehe', 'psd': '1111111', 'result': '1111111'}]
#!/usr/local/bin/python2.7
# encoding: utf-8
import xlrd
import os
"""
读取excel内容
"""
class readExcel(object):
def __init__(self, name):
"""获取当前路径"""
curpath = os.path.dirname(__file__)
"""获取excel文件【与当前脚本在同一级目录下】"""
self.filename = os.path.join(curpath, name)
self.excel_handle = xlrd.open_workbook(self.filename) # 路径不包含中文
# sheet1 = self.excel_handle.sheet_names()[1]