python 使用pandas 操作excel,同时写入数据到多个Sheet

使用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)
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值