Pandas 读取 Excel 斜着读

读取 Excel 斜着读数据

python 复制代码
import pandas as pd


def read_sideling(direction, sheet_name, row_start, col_start, gap):
    """
    斜着读数据
    :param sheet_name:
    :param direction: left 往左下方读取 ↙,right 往右下方读取 ↘
    :param row_idx:  行号,从0开始,
    :param col_idx:  列号,从0开始
    :return:
    """
    # header=None 没有标题行
    df = pd.read_excel("222.xlsx", sheet_name=sheet_name, header=None)
    step = gap + 1
    total = 0
    continuous = 5  # 连续几连,算有效值
    col_idx = col_start  # 第一次,列号=传入的值
    # 如果3行3行的比,再在上面套一层 for
    for row_idx in range(row_start, step * continuous + 1, step):  # range(1, 4) = 1~3 逗号右边是小于,所以 < 4 ,是 1~3,不包括4
        if row_idx == 0 :
            # 如果指定的行是从0开始,就跳过,否则不用跳过
            continue  # 跳过第1行
        # print(row)
        val = df.iloc[row_idx, col_idx]  # 指定行,列
        # TODO 判断 1
        # if val != 1:
        #     print("第%s行,第%s列  =>  %s" % (row_idx + 1, col_idx, val))
        #     break  # 三行数据,有一个不等于 1 就跳过
        total = total + 1
        # 列号位移
        if direction == "right":
            # 从右往左,列的序号 加 1
            col_idx = col_idx + 1
        if direction == "left":
            # 从右往左,列的序号 减 1
            col_idx = col_idx - 1
        print("第%s行,第%s列 =>  %s" % (row_idx + 1, col_idx, val))
    # 三行数据都 =1
    if total == continuous:
        # 'a' 追加到文件中, 'w' 以前的数据不要了,每次都重写
        write_val = df.iloc[0, col_idx]
        print("达到连续三个1,将值写文件%s" % write_val)
        with open('output.txt', 'a', encoding='utf-8') as json_file:
            json_file.write(str(write_val) + " ")  # 把最上面的值写到文件中
    else:
        print("没达到三个1 不写文件")


# 斜着读数据,如果要和竖着的一起处理,就把方法复制到一个文件中
if __name__ == '__main__':
    # ritht 向台
    read_sideling(direction='right', sheet_name=0, row_start=1, col_start=1, gap=0)
    # left 向左
    read_sideling(direction='left', sheet_name=0, row_start=1, col_start=10, gap=0)
    # left 向左
    read_sideling(direction='left', sheet_name=0, row_start=1, col_start=14, gap=2)
相关推荐
小钱c72 小时前
Python使用 pandas操作Excel文件并新增列数据
python·excel·pandas
虎头金猫2 天前
我的远程开发革命:从环境配置噩梦到一键共享的蜕变
网络·python·网络协议·tcp/ip·beautifulsoup·负载均衡·pandas
悟乙己3 天前
PandasAI :使用 AI 优化你的分析工作流
人工智能·pandas·pandasai
weixin_456904275 天前
# Pandas 与 Spark 数据操作完整教程
大数据·spark·pandas
dlraba8026 天前
Pandas:机器学习数据处理的核心利器
人工智能·机器学习·pandas
猫头虎8 天前
如何查看局域网内IP冲突问题?如何查看局域网IP环绕问题?arp -a命令如何使用?
网络·python·网络协议·tcp/ip·开源·pandas·pip
peter67688 天前
pandas学习小结
学习·pandas
猫头虎8 天前
如何解决 pip install -r requirements.txt extras 语法 ‘package[extra’ 缺少 ‘]’ 解析失败问题
开发语言·python·开源·beautifulsoup·virtualenv·pandas·pip
MoRanzhi12039 天前
15. Pandas 综合实战案例(零售数据分析)
数据结构·python·数据挖掘·数据分析·pandas·matplotlib·零售
eqwaak010 天前
数据预处理与可视化流水线:Pandas Profiling + Altair 实战指南
开发语言·python·信息可视化·数据挖掘·数据分析·pandas