栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas读写Excel文件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas读写Excel文件

# coding:utf-8
import pandas as pd
import os


class Reader:
    """
    读取csv文件或xlsx文件
    """
    def __init__(self, path, cols):
        self.path = path
        self.cols = cols
        if not os.path.exists(self.path):
            raise FileNotFoundError("要读取的文件{}不存在,请重新选择!".format(self.path))

    def csv_reader(self, code):
        """
        读取CSV文件
        :return: 返回DataFrame
        """
        print(">>> 读取文件{}".format(self.path))
        chunk = pd.read_csv(self.path, sep=",", usecols=self.cols, chunksize=10000, encoding=code,
                            engine="python", header=0)
        df_csv = pd.DataFrame(pd.concat(chunk, ignore_index=True))
        print(">>> complete!")
        return df_csv

    def excel_reader(self):
        """
        读取Excel文件,支持xls、xlsx、xlsm,默认xlsx
        :return:返回OrderedDict(Sheet_name,DataFrame)
        """
        print(">>> 读取文件{}".format(self.path))
        df_xls = pd.read_excel(self.path, sheet_name=None, usecols=self.cols, engine=None, header=0)
        print(">>> complete!")
        return df_xls


class Writer:
    """
    写入输出csv文件或xlsx文件
    """
    def __init__(self, dataframe, path):
        self.dataframe = dataframe
        self.path = path
        self.mode = 'w'
        if os.path.exists(self.path):
            self.mode = 'a'

    def csv_writer(self, code):
        """
        写入CSV文件
        :return:返回CSV文件
        """
        print(">>> 写入文件{}".format(self.path))
        self.dataframe.to_csv(self.path, encoding=code, index=False)
        print(">>> complete!")

    def excel_writer(self, sht_name):
        """
        写入Excel文件,仅支持xlsx
        :return:返回Excel文件
        """
        try:
            # print(">>> 写入文件:" + self.path)
            print(">>> 向文件{}中写入{}".format(self.path, sht_name))
            with pd.ExcelWriter(self.path, mode=self.mode, engine='openpyxl') as writer:
                self.dataframe.to_excel(excel_writer=writer, sheet_name=sht_name, index=False)
            print(">>> complete!")
        except Exception:
            raise FileNotFoundError("要写入的文件{}路径有问题,请检查路径!".format(self.path))
# coding:utf-8
import file_reader_and_writer as rw

if __name__ == '__main__':
    # 读写csv
    open_path = r'D:2月.csv'
    save_path = r'D:2月(简版).csv'
    # file_cols = None
    file_cols = ['标题1','标题2','标题3']
    file_code = 'ANSI'
    df_Reader = rw.Reader(path=open_path, cols=file_cols)
    df_new = df_Reader.csv_reader(file_code)
    dfSuited = df_new[(df_new["标题1"] == "a") | (df_new["标题2"] == "b") | (df_new["标题3"] == "c")]
    df_writer = rw.Writer(dataframe=dfSuited, path=save_path)
    df_writer.csv_writer(code=file_code)
    # 读写Excel
    # open_path = r'D:表1.xlsx'
    # save_path = r'D:表2.xlsx'
    # df_Reader = rw.Reader(path=open_path, cols=file_cols)
    # df_new = df_Reader.excel_reader()
    # for sheet in df_new.keys():
    #     df_writer = rw.Writer(dataframe=df_new.get(sheet), path=save_path)
    #     df_writer.excel_writer(sht_name=sheet)
    print('执行完毕!')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号