使用pandas操作excel,同时写入数据到多个Sheet
def write_excel_pd(file, sheet, data):
"""
excel title: name / age
df1 = {
'name': [1, 2, 3],
'age': [1, 2, 3]
}
pandas DataFrame write to excel
:param file: file_path + file_name
:param sheet: type:list
:param data: type: list DataFrame
:return:
"""
writer = pd.ExcelWriter(file)
for i in range(len(sheet)):
df = pd.DataFrame(data[i])
df.to_excel(excel_writer=writer, sheet_name=sheet[i])
writer.save()
writer.close()
log.info('写入数据完成!')
读取逐行txt文件内容,并移除特殊字符
def read_txt(file_path, st, rst):
"""
read txt
:param file_path:
:param st: 移除字符串头尾指定字符
:param rst: 删除字符串末尾指定的字符
:return:
"""
content_lst = []
if os.path.exists(file_path):
with open(file_path, 'r+') as f:
for line in f:
content_lst.append(line.strip(st).rstrip(rst))
return content_lst
写入字符串到html文件
def write_html(filename, content):
if os.path.isdir(FILE_PATH):
with open(FILE_PATH + filename, 'w', encoding='utf-8') as f:
f.write("<meta charset='utf-8'>") # 处理html 中文乱码
f.write(content)
log.info('写入数据到 %s' % filename)
else:
log.info('写入失败 路径不存在:%s' % FILE_PATH)