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)  # 不间隔
相关推荐
LilySesy2 天前
【SAP-MOM项目】二、接口对接(中)
开发语言·python·pandas·restful·sap·abap
qq_381454997 天前
Python Pandas完全指南:从核心数据结构到实战操作
pandas
TUTO_TUTO8 天前
【python-词汇指标提取工具开发】自学笔记(1)-后端程序
人工智能·python·pandas·visual studio code
葱明撅腚8 天前
shapely空间数据分析
python·pandas·gis·shapely
忘忧记9 天前
pandas基础三
pandas
叫我:松哥11 天前
基于scrapy的网易云音乐数据采集与分析设计实现
python·信息可视化·数据分析·beautifulsoup·numpy·pandas
测试摆渡媛11 天前
Excel模板填充工具(工具&脚本分享)
python·数据挖掘·pandas
_Soy_Milk11 天前
【算法工程师】—— Python 数据分析
python·数据分析·numpy·pandas·matplotlib
Data-Miner12 天前
类似Pandas AI的几个数据分析处理智能体介绍
人工智能·数据分析·pandas
智航GIS14 天前
11.18 自定义Pandas扩展开发指南:打造你的专属数据分析武器库
python·数据分析·pandas