python找出Excel文件大于2048个字符长度的数据

python找出Excel文件大于2048个字符长度的数据

需求:在一份Excel文件上找出它每个列中字符长度大于2048的内容并把该对应内容的第一个、第二个列数据打印出来和大于2048列的内容

python代码实现

python 复制代码
# -*- coding: utf-8 -*-
# @File  : B.py
# Project:pycharmPOJO
# @Author: Administrator
# E-mail: 3219259586.@qq.com
# @Phone: 191149397XX/199680878XX
# Created by Administrator on 2024/11/22 10:02
# @Date  : 2024-11-22 10:02:39
# @Software: PyCharm
import pandas as pd
import time

start_time = time.time()
print("程序开始时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time)))


def check_row_length(excel_file):
    # 读取 Excel 文件
    df = pd.read_excel(excel_file)

    # 初始化一个标志,表示是否有单元格长度超过 2048 字符
    any_cell_exceeds = False

    # 遍历每一行
    for index, row in df.iterrows():
        # 遍历每一列
        for col_name, cell in row.items():
            # 检查单元格的长度是否超过 2048 字符
            if len(str(cell)) > 2048:
                any_cell_exceeds = True
                # 打印超过 2048 字符的列名和对应的行数据
                first_col_name = df.columns[0]
                second_col_name = df.columns[1]
                first_col_value = row[first_col_name]
                second_col_value = row[second_col_name]
                # print(f"行索引: {index}, 列名: {col_name}, 单元格长度: {len(str(cell))}")
                print(f"\033[91m行索引: {index + 1}, 超过2048字符长度的列名: {col_name}, 单元格长度: {len(str(cell))}\033[0m")
                print(f"第一列: {first_col_name}, 第一列值: {first_col_value}")
                print(f"第二列: {second_col_name}, 第二列值: {second_col_value}")
                print(f"超过 2048 字符的数据内容: {cell}\n")

    # 如果没有单元格长度超过 2048,打印消息
    if not any_cell_exceeds:
        print("没有单元格长度超过 2048 字符。")


# 调用函数
check_row_length('FamilyComplaintsGx20241127085330947.xlsx')  # 替换为您的Excel文件路径
end_time = time.time()
print("程序结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time)))
run_time = end_time - start_time
print("程序运行耗时:%0.2f" % run_time, "s")

运行截图效果

相关推荐
IVEN_13 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang14 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮14 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling14 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮17 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽18 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽2 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers