精准读取CSV/Excel数据 - 灵活指定行列范围的 Python 解决方案

文章目录

  • 源代码
  • 项目简介
  • 导入相关库
  • __file_exists 装饰器
  • 函数的签名和注释
  • 主要功能的实现
  • 运行演示
  • 读取 Excel 文件

源代码

https://github.com/ma0513207162/PyPrecip。pyprecip\reading\read_api.py 路径下。

项目简介

PyPrecip 是一个专注于气候数据处理的 Python 库,旨在为用户提供方便、高效的气候数据处理和分析工具。该库致力于处理各种气候数据,并特别关注于降水数据的处理和分析。

在这里插入图片描述

导入相关库

在这里,读取 csv 和 excel 文件分别使用 csv 库和 openpyxl 库。utits 路径下是笔者自定义的一些工具函数,在源码中可以找到。

import os, csv 
from openpyxl import load_workbook
from ..utits.except_ import RaiseException as exc 
from ..utits.warn_ import RaiseWarn as warn 
from ..utits.sundries import check_param_type

__file_exists 装饰器

一个简易的 python 装饰器 __file_exists,用于检测 path 参数有无正常输入,否则抛出自定义异常。

def __file_exists(func):
    def wrapper(*args, **kwargs):     
        if not args and not kwargs:
            exc.raise_exception("The file path is required.", TypeError) 
        return func(*args, **kwargs)
    return wrapper 

函数的签名和注释

  • 以 read_csv 函数为示例,定义了一些必要的参数。
  • by_row 参数表示是否按行读取数据。
  • 其中 row_indices 和 column_indices 参数为 tuple 类型时,表示指定行索引或列索引。指定为 list 类型时,表示指定行范围或列范围。
  • check_param_type 函数负责检查参数的类型。
@__file_exists    
def read_csv(path: str, by_row: bool = False, 
            row_indices: (tuple|list) = (), column_indices: (tuple|list) = ()): 
    """     
    Reads data for specified rows and columns from a CSV file.

    Parameters:
     - path: indicates the path of the CSV file
     - row_indices: Specifies the read row range (list length 2) or row index (tuple)
     - column_indices: specifies the read column range (list length 2) or column index (tuple)
     - by_row: If True, read the file by rows (default). If False, read the file by columns.
    Returns:
     - Dictionary. The key indicates the file name and the value indicates the read data
    """

    # 检查参数类型 
    check_param_type(path, str, "path");
    check_param_type(row_indices, (tuple|list), "row_indices"); 
    check_param_type(column_indices, (tuple|list), "column_indices");

主要功能的实现

根据传入的 row_indices 和 column_indices 参数搭配 zip 函数进行行列的转换, 使用切片和索引操作从原始 CSV 数据中提取指定的行列数据区域。这里最大的特点就是避免了使用大量的 for 循环进行同样功能的实现。

	read_csv_result: dict = {}; 
    with open(path, "r", encoding="gbk") as csv_file:
        reader_csv = list(csv.reader(csv_file)) 

    # 指定行范围  
    if isinstance(row_indices, list) and row_indices != []:
        if len(row_indices) == 2:
            start, end = row_indices[0], row_indices[1]
            reader_csv = reader_csv[start-1: end]
        else:
            warn.raise_warning("The row_indices parameter must contain only two elements, otherwise it is invalid.") 
    
    # 指定行索引 
    if isinstance(row_indices, tuple) and row_indices != ():
        row_idx_list = []
        for idx in row_indices:
            if idx >= 1 and idx <= len(reader_csv):
                row_idx_list.append(reader_csv[idx-1]) 
            else:
                exc.raise_exception("The index must be greater than 0 and less than the sequence length.", IndexError)
        reader_csv = row_idx_list; 

    # list 类型指定行范围
    reader_csv = list(zip(*reader_csv)) 
    if isinstance(column_indices, list) and column_indices != []:
        if len(column_indices) == 2:
            start, end = column_indices[0], column_indices[1]; 
            reader_csv = reader_csv[start-1: end]
        else:
            warn.raise_warning("The column_indices parameter must contain only two elements, otherwise it is invalid.") 
    
    # tuple 类型指定列索引 
    if isinstance(column_indices, tuple) and column_indices != ():
        col_idx_list = [] 
        for idx in column_indices:
            if idx >= 1 and idx <= len(reader_csv):
                col_idx_list.append(reader_csv[idx-1]); 
            else:
                exc.raise_exception("The index must be greater than 0 and less than the sequence length.", IndexError)
        reader_csv = col_idx_list;
    
    # 按行读取 
    if by_row:
        reader_csv = list(zip(*reader_csv)) 

    # 封装 dict 对象
    file_name = os.path.splitext(os.path.basename(path))[0]; 
    read_csv_result[file_name] = reader_csv; 

    return read_csv_result;

运行演示

# 以主进程的方式运行 
if __name__ == "__main__": 
    path = "./static/test_data.xlsx"
    read_result = read_excel(path=path, row_indices=(1,3), column_indices=[1,5]);

    for key in read_result:
        for value in read_result[key]:
            print(value)

在这里插入图片描述

读取 Excel 文件

同样的逻辑,也适用于读取 Excel 文件。

@__file_exists    
def read_excel(path: str, by_row: bool = False,  sheet_names: tuple = (), row_indices: (tuple|list) = (),
            column_indices: (tuple|list) = ()) -> dict: 
    """
    Reads data from a specified worksheet, row, and column from an Excel file.

    Parameters:
     - path: indicates the Excel file path
     - sheet_names: A tuple of sheet names to be read. If empty, the active sheet is read
     - row_indices: Specifies the read row range (list length 2) or row index (tuple)
     - column_indices: specifies the read column range (list length 2) or column index (tuple)
     - by_row: If True, read the file by rows (default). If False, read the file by columns.
    Return:
     - Dictionary. The key is the name of the worksheet and the value is the read data
    """

    # 检查参数类型 
    check_param_type(path, str, "path");
    check_param_type(sheet_names, tuple, "sheet_names");
    check_param_type(row_indices, (tuple|list), "row_indices"); 
    check_param_type(column_indices, (tuple|list), "column_indices");

    workbook = load_workbook(filename = path, data_only = True) 

    # Gets the specified worksheet 
    sheet_list = []
    if sheet_names != ():
        for sheet_name in sheet_names:
            sheet_list.append(workbook[sheet_name])
    else:
        sheet_list.append(workbook.active) 

    read_excel_result: dict = {}; 
    # 遍历工作簿 sheet_list
    for sheet in sheet_list:
        sheet_iter_rows: list = list(sheet.iter_rows(values_only = True)) 
        
        # 指定行范围 
        if isinstance(row_indices, list) and row_indices != []:
            if len(row_indices) == 2:
                start, end = row_indices[0], row_indices[1] 
                sheet_iter_rows = sheet_iter_rows[start-1: end]
            else:
                warn.raise_warning("The row_indices parameter must contain only two elements, otherwise it is invalid.") 

        # 指定行索引 
        if isinstance(row_indices, tuple) and row_indices != ():
            temp_iter_rows = []
            for idx in row_indices:
                if idx >= 1 and idx <= len(sheet_iter_rows):
                    temp_iter_rows.append(sheet_iter_rows[idx-1]) 
                else:
                    exc.raise_exception("The index must be greater than 0 and less than the sequence length.", IndexError)
            sheet_iter_rows = temp_iter_rows   

        # list 类型指定行范围
        sheet_iter_cols = list(zip(*sheet_iter_rows)) 
        if isinstance(column_indices, list) and column_indices != []:
            if len(column_indices) == 2:
                start, end = column_indices[0], column_indices[1]; 
                sheet_iter_cols = sheet_iter_cols[start-1: end]  
            else:
                warn.raise_warning("The column_indices parameter must contain only two elements, otherwise it is invalid.")   

        # tuple 类型指定列索引 
        if isinstance(column_indices, tuple) and column_indices != ():
            col_idx_list = [] 
            for idx in column_indices:
                if idx >= 1 and idx <= len(sheet_iter_cols):
                    col_idx_list.append(sheet_iter_cols[idx-1]); 
                else:
                    exc.raise_exception("The index must be greater than 0 and less than the sequence length.", IndexError)
            sheet_iter_cols = col_idx_list; 
        
        # 是否按行读取 
        if by_row:
            sheet_iter_cols = list(zip(*sheet_iter_cols)) 

        read_excel_result[sheet.title] = sheet_iter_cols; 

    return read_excel_result;  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/597649.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【C++ 关键字】const 关键字详解

文章目录 1. const 概念2.常量指针 和 指针常量 的区别2.1 常量指针&#xff08;底层 const&#xff09;2.2 指针常量 (顶层 const) 3.const 关键字的作用4.const 和 define 的区别5.const 总结 1. const 概念 const 是一个关键字&#xff0c;被修饰的值不能改变&#xff0c;是…

请求转发和响应重定向

文章目录 一、 概述二、 请求转发三、响应重定向参考资料 一、 概述 什么是请求转发和响应重定向 请求转发和响应重定向是web应用中间接访问项目资源的两种手段,也是Servlet控制页面跳转的两种手段 请求转发通过HttpServletRequest实现,响应重定向通过HttpServletResponse实现…

大模型模型简化机器人训练;简单易用的 3D 工具Project Neo;特斯拉放出了擎天柱机器人最新训练视频

✨ 1: DrEureka 利用大语言模型自动化将机器人仿真环境训练结果转移到真实世界 DrEureka是一种利用大型语言模型&#xff08;LLMs&#xff09;自动化和加速从仿真&#xff08;sim&#xff09;到现实世界&#xff08;real&#xff09;转移的技术。在机器人技能学习领域&#x…

Gradle基础学习(六) 认识任务Task

理解Gradle中的任务 Gradle的构建过程基于任务&#xff08;Task&#xff09;的概念&#xff0c;而每个任务都可以包含一个或多个动作&#xff08;Action&#xff09;。 任务是构建中执行的一些独立的工作单元&#xff0c;例如编译类、创建JAR、生成Javadoc或将存档发布到仓库…

62-USB转JTAG or SPI电路设计

视频链接 USB转JTAG or SPI电路设计01_哔哩哔哩_bilibili USB 转 JTAG or SPI电路设计 第07课---USB转串口电路设计第 34&#xff5e;40课---USB硬件电路设计 第22课---SPI Flash电路设计 第31课---JTAG电路设计&#xff08;JLINK&XILINX&ALTERA&#xff09; 第…

代码随想录-算法训练营day31【贪心算法01:理论基础、分发饼干、摆动序列、最大子序和】

代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客 第八章 贪心算法 part01● 理论基础 ● 455.分发饼干 ● 376. 摆动序列 ● 53. 最大子序和 贪心算法其实就是没有什么规律可言&#xff0c;所以大家了解贪心算法 就了解它没有规律的本质就够了。 不用花心思去研究其…

5.Git

Git是一个分布式版本控制工具&#xff0c;主要用于管理开发过程中的源代码文件&#xff08;Java类、xml文件、html文件等&#xff09;。通过Git仓库来存储和管理这些文件&#xff0c;Git仓库分为两种 本地仓库&#xff1a;开发人员自己电脑上的Git仓库远程仓库&#xff1a;远程…

360手机去除广告 360手机关闭弹窗广告 360手机刷机

360手机去除广告 360手机关闭弹窗广告 360手机刷机 360手机去广告 360手机刷机 360手机弹窗广告 永久去除360手机的各种广告教程 360手机禁止更新 360手机关闭广告 360手机去除内部广告 360手机资源网 360手机刷机资源下载链接&#xff1a;360rom.github.io 参考&#xff1a;…

如何高效封装App?小猪APP分发平台一站式解决方案

在移动应用开发领域&#xff0c;App封装&#xff08;App Packaging&#xff09;是一个至关重要的环节&#xff0c;它不仅关乎应用的安全性&#xff0c;还直接影响到最终用户体验和市场推广策略。本文旨在通过实战指南&#xff0c;揭示如何高效完成App封装&#xff0c;并介绍如何…

【图书推荐】《图神经网络基础、模型与应用实战》

本书目的 详解PyTorch 图神经网络基础理论、模型与十多个应用案例&#xff0c;带领读者掌握图神经网络在自然语言处理、计算机视觉、推荐系统、社交网络4个领域的应用开发方法&#xff0c;丰富读者利用深度学习算法解决实际问题的能力。 本书案例 图卷积网络实现图注意力网络…

Python量化炒股的统计数据图

Python量化炒股的统计数据图 单只股票的收益统计图 查看单只股票的收盘价信息 单击聚宽JoinQuant量化炒股平台中的“策略研究/研究环境”命令&#xff0c;进入Jupyter Notebook的研究平台。然后单击“新建”按钮&#xff0c;创建Python3文件&#xff0c;输入如下代码如下&am…

知到java笔记(4.1--继承的用法以及this和super的用法)

格式&#xff1a; 例子&#xff1a; get set获取父类的私有变量 private属性 this和super区别&#xff1a; this用法 super用法 例子

星戈瑞CY7-COOH荧光探针,助力生物医学研究

CY7-COOH是一种近红外荧光染料&#xff0c;具有优异的光稳定性、高量子产率和强烈的荧光信号。此外&#xff0c;CY7-COOH还具有较长的激发和发射波长&#xff0c;使其在生物医学成像中具有较高的穿透力和较低的背景干扰。这使得CY7-COOH荧光探针在生物医学研究中具有诸多应用前…

弹性云服务器给用户带来了哪些便利

什么是弹性云服务器&#xff1f; 弹性云服务器&#xff08;ECS&#xff0c;Elastic Cloud Server&#xff09;简单地说&#xff0c;是指运行在云计算环境中的虚拟服务器。弹性云服务器可以说是虚拟专用服务器(VPS)&#xff0c;但VPS却不能说是云服务器。这是因为两者有着本质的…

南京观海微电子---电源,从微观角度观看电功率是怎么产生

从微观角度看看无功功率是怎么产生的&#xff0c;在此之前&#xff0c;我们得先知道引起无功功率的元器件是储能器件&#xff0c;主要是电感和电容。 首先&#xff0c;在宏观上&#xff0c;我们知道电感能导致电压超前电流90&#xff0c;可从如下公式推出&#xff1a; 由此可以…

场景文本检测识别学习 day09(Swin Transformer论文精读)

Patch & Window 在Swin Transformer中&#xff0c;不同层级的窗口内部的补丁数量是固定的&#xff0c;补丁内部的像素数量也是固定的&#xff0c;如上图的红色框就是不同的窗口&#xff08;Window&#xff09;&#xff0c;窗口内部的灰色框就是补丁&#xff08;Patch&#…

量子力学(入门通俗版,转述)

/仅作参考和学习&#xff0c;勿作他用/ 量子力学 量子力学无非就是物理理论。 物理理论就是对自然现象的归纳。------不太容易理解的自然现象。 我们面对的世界&#xff0c;宏观和微观之分。宏观和微观的分界线就是原子。 微观世界和宏观世界没有什么共同点。 牛顿力学用于宏…

14、深入探讨JVM中令人头痛的‘Stop the World’难题

14.1、前文回顾 上一篇文章通过一个实际案例,深入剖析了新生代的对象分配机制,以及对象如何被迁移到老年代。我们还探讨了一个会频繁触发Full GC的场景,并提供了针对性的优化策略,相信大家对JVM的核心运行原理已经理解得相当透彻。 在本文中,我们将讨论一个让Java工程师…

鸿蒙内核源码分析(互斥锁篇) | 互斥锁比自旋锁丰满多了

内核中哪些地方会用到互斥锁?看图: 图中是内核有关模块对互斥锁初始化,有文件,有内存,用消息队列等等,使用面非常的广.其实在给内核源码加注的过程中,会看到大量的自旋锁和互斥锁,它们的存在有序的保证了内核和应用程序的正常运行.是非常基础和重要的功能. 概述 自旋锁 和…

Revit模型移动设备加载优化

BIM/CAD 模型可能包含大量细节&#xff0c;在智能手机和移动 VR 设备上加载时需要特别注意。以下是保持Revit模型整洁的一些步骤&#xff0c;以便任何人都可以毫无问题地加载它们。 NSDT工具推荐&#xff1a; Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 -…
最新文章