使用 Python 的 openpyxl 库可以方便地读取 Excel 文件并进行数据处理。以下代码示例展示了如何筛选出 Excel 文件中大于 1 或小于 -1 的数值,并返回这些数值所在的列号。\n\npython\nimport openpyxl\n\ndef get_columns_with_values_greater_than_1_or_less_than_minus_1(file_path):\n workbook = openpyxl.load_workbook(file_path)\n sheet = workbook.active\n\n columns = []\n\n for column in sheet.iter_cols():\n for cell in column:\n if cell.value is not None and (cell.value > 1 or cell.value < -1):\n columns.append(cell.column_letter)\n\n return columns\n\nfile_path = 'example.xlsx'\ncolumns = get_columns_with_values_greater_than_1_or_less_than_minus_1(file_path)\nprint(columns)\n\n\n请确保已经安装了 openpyxl 库,可以使用以下命令安装:\n\n\npip install openpyxl\n\n\n在上述示例代码中,file_path 变量是 Excel 文件的路径,get_columns_with_values_greater_than_1_or_less_than_minus_1 函数将返回一个包含满足条件的列号的列表。最后,我们打印出这些列号。


原文地址: https://www.cveoy.top/t/topic/pF7k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录