# -*- coding: utf-8 -*-
"""
文件读取。YamlReader读取yaml文件,ExcelReader读取excel,CsvReader读取csv。
"""
import yaml
import os
from xlrd import open_workbook
from csv import reader, DictReader
import xlutils.copy
class YamlReader(object):
"""
读取yaml文件中的内容,返回list
"""
def __init__(self, yamlf):
if os.path.exists(yamlf):
self.yamlf = yamlf
else:
raise FileNotFoundError('文件不存在!')
self.__data = None
@property
def data(self):
# 如果是第一次调用data,读取yaml文档,否则直接返回之前保存的数据
if not self.__data:
with open(self.yamlf, 'rb') as f:
self.__data = list(yaml.safe_load_all(f)) # load后是个generator,用list组织成列表
return self.__data
class SheetTypeError(Exception):
pass
class ExcelReader(object):
"""
读取excel文件中的内容。返回list。
如:
excel中内容为:
| A | B | C |
| A1 | B1 | C1 |
| A2 | B2 | C2 |
如果 print(ExcelReader(excel, title_line=True).data),输出结果:
[{A: A1, B: B1, C:C1}, {A:A2, B:B2, C:C2}]
如果 print(ExcelReader(excel, title_line=False).data),输出结果:
[[A,B,C], [A1,B1,C1], [A2,B2,C2]]
可以指定sheet,通过index或者name:
ExcelReader(excel, sheet=2)
ExcelReader(excel, sheet='BaiDuTest')
"""
def __init__(self, excel, sheet=0, title_line=True):
if os.path.exists(excel):
self.excel = excel
else:
raise FileNotFoundError('文件不存在!')
self.sheet = sheet
self.title_line = title_line
self._data = list()
# 装饰器添加了一个返回参数功能
@property
def data(self):
if not self._data:
workbook = open_workbook(self.excel)
if type(self.sheet) not in [int, str]:
raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet)))
elif type(self.sheet) == int:
s = workbook.sheet_by_index(self.sheet)
else:
s = workbook.sheet_by_name(self.sheet)
if self.title_line:
title = s.row_values(0) # 首行为title
for col in range(1, s.nrows):
# 依次遍历其余行,与首行组成dict,拼到self._data中
self._data.append(dict(zip(title, s.row_values(col))))
else:
for col in range(0, s.nrows):
# 遍历所有行,拼到self._data中
self._data.append(s.row_values(col))
return self._data
def deleteData(self):
if not self._data:
rb = open_workbook(self.excel)
if type(self.sheet) not in [int, str]:
raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet)))
elif type(self.sheet) == int:
s = rb.sheet_by_index(self.sheet)
else:
s = rb.sheet_by_name(self.sheet)
if self.title_line:
# 首行为title
title = s.row_values(0)
# 跳过第二行
for col in range(2, s.nrows):
# 依次遍历其余行,与首行组成dict,拼到self._data中
self._data.append(dict(zip(title, s.row_values(col))))
else:
# 跳过第二行
for col in range(1, s.nrows):
# 遍历所有行,拼到self._data中
self._data.append(s.row_values(col))
# 重新写入数据,然后覆盖保存,达到删除第二行的目的
title = s.row_values(0)
wb = xlutils.copy.copy(rb)
# 获取sheet对象,通过sheet_by_index()获取的sheet对象没有write()方法
ws = wb.get_sheet(0)
n = 0
datas = list(self._data)
# wb = xlwt.Workbook() # 注意Workbook的开头W要大写
ws.write(0, n, title[0])
for i in datas:
n = n + 1
ws.write(n, 0, int(i[title[0]]))
# sheet1 = wb.add_sheet("sheet1", cell_overwrite_ok=True)
# wb.add_sheet('sheetnnn2', cell_overwrite_ok=True)
wb.save(self.excel)
# 装饰器添加了一个返回参数功能
@property
def testdata(self):
if not self._data:
workbook = open_workbook(self.excel)
if type(self.sheet) not in [int, str]:
raise SheetTypeError('Please pass in <type int> or <type str>, not {0}'.format(type(self.sheet)))
elif type(self.sheet) == int:
s = workbook.sheet_by_index(self.sheet)
else:
s = workbook.sheet_by_name(self.sheet)
if self.title_line:
title = s.row_values(0) # 首行为title
for col in range(1, s.nrows):
# 依次遍历其余行,拼到self._data中
self._data.append(s.row_values(col))
else:
for col in range(0, s.nrows):
# 遍历所有行,拼到self._data中
self._data.append(s.row_values(col))
return self._data
class CsvReader(object):
"""
读取csv文件中的内容,返回list
"""
def __init__(self, csvf, title_line=True):
if os.path.exists(csvf):
self.csvf = csvf
else:
raise FileNotFoundError('文件不存在!')
self.title_line = title_line
self.__data = None
@property
def data(self):
# 如果是第一次调用data,读取csv文档,否则直接返回之前保存的数据
if not self.__data:
with open(self.csvf, 'r') as f:
if self.title_line:
self.__data = list(DictReader(f)) # load后是个generator,用list组织成列表
else:
self.__data = list(reader(f))
return self.__data
if __name__ == '__main__':
网友评论