TypeError:'>'在元素的实例之间不支持'和' int'当使用openpyxl卸下行时

发布于 2025-01-29 07:24:00 字数 3437 浏览 5 评论 0 原文

给定一个 .xlsx 具有许多分组/级别:

未扩展:

扩展:

我正在尝试通过 .xlsx 迭代并删除任何行,其中管理样式 is == Management> Management_style_rows_remove中的任何值列表。

valueerror:我在运行脚本时收到此 value

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\WILLIA~1.FOR\AppData\Local\Temp/ipykernel_20732/4051092418.py in <module>
     18                 data_sheet.delete_rows(row)
     19 
---> 20 row_cleansing()

C:\Users\WILLIA~1.FOR\AppData\Local\Temp/ipykernel_20732/4051092418.py in row_cleansing()
     16         for cell in row:
     17             if cell.value in management_style_rows_remove:
---> 18                 data_sheet.delete_rows(row)
     19 
     20 row_cleansing()

~\.conda\envs\JPDevelopment\lib\site-packages\openpyxl\worksheet\worksheet.py in delete_rows(self, idx, amount)
    727         """
    728 
--> 729         remainder = _gutter(idx, amount, self.max_row)
    730 
    731         self._move_cells(min_row=idx+amount, offset=-amount, row_or_col="row")

~\.conda\envs\JPDevelopment\lib\site-packages\openpyxl\worksheet\worksheet.py in _gutter(idx, offset, max_val)
    898     range(cells_to_delete) > range(cell_to_be_moved)
    899     """
--> 900     gutter = range(max(max_val+1-offset, idx), min(idx+offset, max_val)+1)
    901     return gutter

TypeError: '>' not supported between instances of 'tuple' and 'int'

>上面描述的结果:

import openpyxl
from openpyxl import load_workbook

def row_cleansing():

    management_style_rows_remove = ['Corporate', 'Global Core Tax-Loss Harvesting', 'High Yield - Corporate', 'High Yield - Municipal', 'Investment Grade - Agg', 'Investment Grade - Agg ESG',
                               'Investment Grade - Municipal', 'Investment Grade - Municipal ESG', 'Investment Grade TIPS', 'Investment Grade Treasurys', 'MLPs', 'Multi-Asset 55ip', 'Multi-Asset Class',
                               'Non-US Core Tax-Exempt ESG', 'Non-US Core Tax-Exempt ESG FX Hedge', 'Non-US Core Tax-Loss Harvesting ESG']

    input_file = 'annonamized_test_data_to_be_split.xlsx'

    workbook = load_workbook(input_file)
    data_sheet = workbook.active
    
    for row in data_sheet.iter_rows(min_col=3, max_col=3, min_row=2, max_row=None):
        for cell in row:
            if cell.value in management_style_rows_remove:
                data_sheet.delete_rows(row)

row_cleansing()

帮助:我无法准确地将此 valueerror 的含义,因为它与我的脚本有关。

我尝试/观察到的内容:

  1. valueerror 仅在 Management_style_rows_remove中有一个匹配项时,仅标志 list 在中具有值管理样式
  2. valueerror 标志不管我是否引用列表(根据代码),还是我通过对单个值测试代码来通过从中更改代码来测试代码,如果 if cell.value in Management_style_rows_remove中的value: 如果Cell.Value'=='MLPS'

是否有人能够在我的代码上提供提示/提示,以及为什么可能会标记此“ ValueError”?

Given a .xlsx with many Groupings/Levels:

Not expanded:
enter image description here

Expanded:
enter image description here

I am trying to iterate through the .xlsx and remove any rows, where Management Style is == any value in the management_style_rows_remove list.

ValueError: I receive this ValueError when running the script:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\WILLIA~1.FOR\AppData\Local\Temp/ipykernel_20732/4051092418.py in <module>
     18                 data_sheet.delete_rows(row)
     19 
---> 20 row_cleansing()

C:\Users\WILLIA~1.FOR\AppData\Local\Temp/ipykernel_20732/4051092418.py in row_cleansing()
     16         for cell in row:
     17             if cell.value in management_style_rows_remove:
---> 18                 data_sheet.delete_rows(row)
     19 
     20 row_cleansing()

~\.conda\envs\JPDevelopment\lib\site-packages\openpyxl\worksheet\worksheet.py in delete_rows(self, idx, amount)
    727         """
    728 
--> 729         remainder = _gutter(idx, amount, self.max_row)
    730 
    731         self._move_cells(min_row=idx+amount, offset=-amount, row_or_col="row")

~\.conda\envs\JPDevelopment\lib\site-packages\openpyxl\worksheet\worksheet.py in _gutter(idx, offset, max_val)
    898     range(cells_to_delete) > range(cell_to_be_moved)
    899     """
--> 900     gutter = range(max(max_val+1-offset, idx), min(idx+offset, max_val)+1)
    901     return gutter

TypeError: '>' not supported between instances of 'tuple' and 'int'

Code: here is my v.basic script that I expected to achieve the result described above:

import openpyxl
from openpyxl import load_workbook

def row_cleansing():

    management_style_rows_remove = ['Corporate', 'Global Core Tax-Loss Harvesting', 'High Yield - Corporate', 'High Yield - Municipal', 'Investment Grade - Agg', 'Investment Grade - Agg ESG',
                               'Investment Grade - Municipal', 'Investment Grade - Municipal ESG', 'Investment Grade TIPS', 'Investment Grade Treasurys', 'MLPs', 'Multi-Asset 55ip', 'Multi-Asset Class',
                               'Non-US Core Tax-Exempt ESG', 'Non-US Core Tax-Exempt ESG FX Hedge', 'Non-US Core Tax-Loss Harvesting ESG']

    input_file = 'annonamized_test_data_to_be_split.xlsx'

    workbook = load_workbook(input_file)
    data_sheet = workbook.active
    
    for row in data_sheet.iter_rows(min_col=3, max_col=3, min_row=2, max_row=None):
        for cell in row:
            if cell.value in management_style_rows_remove:
                data_sheet.delete_rows(row)

row_cleansing()

Help: I haven't been able to triangulate exactly what this ValueError means, as it relates to my script.

What I've tried/observed:

  1. This ValueError only flags when there is a match in the management_style_rows_remove list with a value in the Management Style in the .xlsx.
  2. This ValueError flags regardless of whether I reference the list (as per the code) or if I test the code by testing against a single value by changing the code from if cell.value in management_style_rows_remove: to if cell.value '== 'MLPs'.

Is anyone able to offer hints/tips on my code and why this `ValueError' might be flagging?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2025-02-05 07:24:00

提示是您使用这些方法进行的任何迭代,您将获得的对象是元组(您可以使用Type函数进行检查)。

任何元组都是不明显的。

您应该考虑使用dict而不是工作。

表示“感谢”是值得赞赏的,但没有回答这个问题。相反,投票最有助于您的答案!如果这些答案对您有帮助,请考虑以更具建设性的方式表示感谢 - 通过对同龄人在这里提出的问题做出自己的答案。

The hint is thar any iteration you do with these methods the object you will get is a tuple (you can check it with type function).

Any tuple is inmutable.

You should consider work with a dict instead of.

Saying “thanks” is appreciated, but it doesn’t answer the question. Instead, vote up the answers that helped you the most! If these answers were helpful to you, please consider saying thank you in a more constructive way – by contributing your own answers to questions your peers have asked here.

回忆那么伤 2025-02-05 07:24:00

我刚才遇到了同样的问题。我在您还必须通过插入索引和.ROW方法来访问该行。

这是对我有用的例子。在第5列的行中迭代时,如果单元格值中有“时间表”,我想删除当前行,还有3个其他行:

#Deleting those breaker lines
for row in ws.iter_rows(min_col=5, max_col=5):
    for cell in row:
        if 'Schedule' in str(cell.value):
            ws.delete_rows(row[0].row,4)

在您的情况下,我相信它会起作用,如果

以下块:

    for row in data_sheet.iter_rows(min_col=3, max_col=3, min_row=2, max_row=None):
    for cell in row:
        if cell.value in management_style_rows_remove:
            data_sheet.delete_rows(row)

您更改了最后一点:

data_sheet.delete_rows(row[0].row)

希望它像对我一样有帮助!

I've had the same problem just now. I figured in someone else's code that you have to access the row by inserting an index and .row method as well.

Here is an example of what has worked for me. When iterating through the rows of column 5, if the cell value has 'Schedule' in it, I want to delete the current row and 3 more others:

#Deleting those breaker lines
for row in ws.iter_rows(min_col=5, max_col=5):
    for cell in row:
        if 'Schedule' in str(cell.value):
            ws.delete_rows(row[0].row,4)

In your case, I believe it would work if:

In the following block:

    for row in data_sheet.iter_rows(min_col=3, max_col=3, min_row=2, max_row=None):
    for cell in row:
        if cell.value in management_style_rows_remove:
            data_sheet.delete_rows(row)

You changed the last bit to:

data_sheet.delete_rows(row[0].row)

Hope it helps just like it helped me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文