Pandas 读取Eexcel - 间隔N行,读取某列数据

间隔N行,读取某列数据

python 复制代码
import pandas as pd

def read_vertical(sheet_name, col_idx, gap):
    """
    竖着读数据,
    :param sheet_name: 第几个 sheet
    :param col_idx: 第几列,从 0 开始
    :param step: 间隔
    :return:
    """
    # header=None 没有标题行
    df = pd.read_excel("111.xlsx", sheet_name=sheet_name, header=None)
    step = gap + 1
    total = 0
    # 如果3行3行的比,再在上面套一层 for
    for row in range(0, step * 3 + 1, step):  # range(1, 4) = 1~3 逗号右边是小于,所以 < 4 ,是 1~3,不包括4
        if row == 0:
            continue  # 跳过第1行
        # print(row)
        val = df.iloc[row, col_idx]  # 指定行,列
        if val != 1:
            print("第%s行 =>  %s,不等于 1 所以跳过。" % (row + 1, val))
            break  # 三行数据,有一个不等于 1 就跳过
        total = total + 1
        print("第%s行 =>  %s" % (row + 1, val))
    # 三行数据都 =1
    if (total == 3):
        # '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__':
    for col in range(1, 2):
        print('\r\n' + '=' * 10 + ' 第' + str(col) + '列')
        for gap in range(0, 6):
            print('\r\n' + '*' * 10 + ' 间隔' + str(gap))
            read_vertical(0, col, gap)  # 不间隔
相关推荐
人工干智能1 小时前
科普:Pandas 索引器 loc 与 iloc 的编程思维
java·人工智能·pandas
梅雅达编程笔记2 小时前
编程启蒙|Scratch 转 Python 系列第9天:字典/哈希表积木双向对照(AI大模型参数配置表实战)
开发语言·人工智能·python·numpy·pandas
benchmark_cc1 天前
如何用 Python + QuantDash 快速构建高胜率“配对交易(Pairs Trading)”策略?
开发语言·人工智能·python·pandas·量化交易·quantdash
weixin_307779134 天前
Python Pandas读取Excel Sheet生成SQL WHERE条件
python·sql·自动化·excel·pandas
梅雅达编程笔记4 天前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas
你想知道什么?8 天前
Pandas学习笔记
笔记·学习·pandas
猫头虎10 天前
城市级IP代理:赋能全球企业本地化数字增长与安全合规 | Decodo 德口多
网络·python·网络协议·tcp/ip·安全·pandas·pip
李昊哲小课1 个月前
PyArrow 完整教程
大数据·数据分析·pandas·pyarrow
云和数据.ChenGuang1 个月前
T5大模型
人工智能·机器人·pandas·数据预处理·数据训练
MATLAB代码顾问1 个月前
Python Pandas数据分析入门指南
python·数据分析·pandas