【python】读取csv xlsx xlx txt文件 类

只需修改path

class Reader:
    """
    可读取的文件格式: .csv .tsv .xlsx .xlx .txt
    """

    @staticmethod
    def change_1d_array(array, header_cut=None, str_to_float=None):
        """
        为一维数组去掉第一个值;将字符串转为数值
        :param array: type:<class 'numpy.ndarray'> shape:(n,)
        :param header_cut: Ture/None 是否需要第一个值
        :param str_to_float: Ture/None 是否需要字符转数值
        :return: 二维数组
        """
        if header_cut != None:
            arr = list(array)
            arr.remove(arr[0])
            array = np.array(arr)
        if str_to_float != None:
            array = np.array(list(map(float, array)))

        return array

    @staticmethod
    def change_2d_array(array, header_cut=None, str_to_float=None):
        """
        为二维数组去掉第一行;将字符串转为数值
        :param array: type:<class 'numpy.ndarray'> shape:(n, n)
        :param header_cut: Ture/None 是否需要第一行
        :param str_to_float: Ture/None 是否需要字符转数值
        :return: 二维数组
        """
        if header_cut != None:
            arr = list(array)
            arr.remove(arr[0])
            array = np.array(arr)
        if str_to_float != None:
            array = array.astype(np.float64)

        return array

    @staticmethod
    def get_rows_cols(data):
        """
        拿到二维数组的每一行和每一列(此处假设每一行的列数相同)
        :param data: type: <class 'numpy.ndarray'>   shape:(n,n)
        :return:rows:every rows  cols:every cols
        """
        rows = []   # 每一行作为元素
        cols = []   # 每一列作为元素
        for value in data:
            rows.append(value)
        for i in range(len(data[0])):
            cols.append(data[:, i])
        # 转为np数组
        rows = np.array(rows)
        columns = np.array(cols)

        return rows, columns

    @staticmethod
    def read_csv(path):
        """
        读取.csv或.tsv文件
        :param path:文件路径
        :return:二维数组
        """
        array = pd.read_csv(path, header=None)
        np_array = np.array(array)
        return np_array

    @staticmethod
    def read_xlsx(path, num_of_sheet):
        """
        读取.xlsx或.xlx文件,为二维数组
        :param path: 文件路径
        :param num_of_sheet: 表数
        :return: 二维数组
        """
        # 读取文件
        wb = load_workbook(path)
        # 获取当前所有的sheet
        sheets = wb.worksheets
        # 选择某个sheet
        sheet = sheets[num_of_sheet]
        # 获得这个sheet的行
        rows = sheet.rows
        # 迭代读取所有的行,每一行作为一个列表
        data = []
        for row in rows:
            data.append([col.value for col in row])
        data = np.array(data)
        return data

    @staticmethod
    def read_txt(path):
        """
        1.目前.txt文件里每一行的相隔只能是','或者'\t'
        :param path: 文件路径
        :return: 二维数组
        """
        data = np.loadtxt(path)
        return data


if __name__ == '__main__':
    reader = Reader()   # 实例化
    path = './y.xlsx'    # 路径
    data = reader.read_xlsx(path, 0)    # 读取路径下的.xlsx的第0个表
    data = reader.change_2d_array(data, header_cut=True, str_to_float=True)  # 将第一行去掉,然后将所有字符串转为float
    # 1.原始数据
    x = data[:, 4]
    y = data[:, 5]
    ps = data[:, 6]
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司六米希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值