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")

运行截图效果

相关推荐
消失的旧时光-19434 分钟前
Kotlin when 用法完整分享
android·开发语言·kotlin
万粉变现经纪人4 分钟前
如何解决 pip install -r requirements.txt 约束文件 constraints.txt 仅允许固定版本(未锁定报错)问题
开发语言·python·r语言·django·beautifulsoup·pandas·pip
站大爷IP10 分钟前
Python定时任务实战:APScheduler从入门到精通
python
Fairy_sevenseven17 分钟前
[1]python爬虫入门,爬取豆瓣电影top250实践
开发语言·爬虫·python
ThisIsMirror20 分钟前
CompletableFuture并行任务超时处理模板
java·windows·python
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 计算图和 tf.function 简介
python·深度学习·tensorflow·tensorflow2
程序员晚枫1 小时前
Python 3.14新特性:Zstandard压缩库正式加入标准库,性能提升30%
python
逆境清醒1 小时前
VS Code配置Python开发环境系列(1)___VScode的安装 ,VScode常用快捷键
vscode·python·visual studio code
珹洺1 小时前
Java-Spring入门指南(二十一)Thymeleaf 视图解析器
java·开发语言·spring
Predestination王瀞潞1 小时前
类的多态(Num020)
开发语言·c++