import pandas as pd

class CSVFileUtil:
    def __init__(self, filepath):
        """
        初始化CSV工具类
        :param filepath: CSV文件的路径
        """
        self.filepath = filepath

    def read_csv(self):
        """
        读取CSV文件
        :return: 返回DataFrame对象
        """
        return pd.read_csv(self.filepath)
    
    def read_csv_columns(self,useColumns):
        """
        读取CSV文件
        :return: 返回DataFrame对象
        """
        return pd.read_csv(self.filepath,usecols=useColumns)

    def write_csv(self, data, dest_path):
        """
        将数据写入CSV文件
        :param data: DataFrame对象或可转换为DataFrame的数据
        :param dest_path: 保存CSV文件的路径
        """
        if not isinstance(data, pd.DataFrame):
            data = pd.DataFrame(data)
        data.to_csv(dest_path, index=False)

    def select_and_save_columns(self, columns, dest_path):
        """
        选取指定的列并保存为新的CSV文件
        :param columns: 要选取的列名列表
        :param dest_path: 保存新CSV文件的路径
        """
        df = self.read_csv_columns(columns)
        self.write_csv(df, dest_path)

# 使用示例
if __name__ == "__main__":
    filepath = '/path/to/your/original.csv'  # 替换为你的CSV文件路径
    dest_path = '/path/to/your/selected_columns.csv'  # 替换为你想保存选取列后的CSV文件路径
    columns = ['时间','iTempOutdoor_1sec','iTempNacelle_1sec','iGenPower','iRotorSpeedPDM','iGenSpeed','iWindSpeed_real',
               'iPitchAngle1','iPitchAngle2','iPitchAngle3','iNacellePositionTotal','iwindDirection','iVaneDiiection',
               'iActivePoweiSetPointValue','iReactivePower','iKWhThisDay_h','iVibrationY','iVibrationZ','iTemp1GearOil_1sec',
               'iTempRotorBearA_1sec','iTempGearBearNDE_1sec','iTempGearBearDE_1sec','iTempGenBearDE_1sec','iTempGenBearNDE_1sec',
               'iTempGenStatorU_1sec','iTempHub_1sec','iTempCntr_1sec','WT_Runcode','WT_Faultcode','iTempRotorBearA_1sec']  # 替换为你想选取的列名

    tool = CSVFileUtil(filepath)
    tool.select_and_save_columns(columns, dest_path)


