TypeError:'>'在元素的实例之间不支持'和' int'当使用openpyxl卸下行时
给定一个 .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
的含义,因为它与我的脚本有关。
我尝试/观察到的内容:
- 此
valueerror
仅在Management_style_rows_remove中有一个匹配项时,仅标志
list 在中具有值管理样式
。 - 此
valueerror
标志不管我是否引用列表(根据代码),还是我通过对单个值测试代码来通过从中更改代码来测试代码,如果
if cell.value in Management_style_rows_remove中的value:
如果Cell.Value'=='MLPS'
。
是否有人能够在我的代码上提供提示/提示,以及为什么可能会标记此“ ValueError”?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提示是您使用这些方法进行的任何迭代,您将获得的对象是元组(您可以使用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.
我刚才遇到了同样的问题。我在您还必须通过插入索引和.ROW方法来访问该行。
这是对我有用的例子。在第5列的行中迭代时,如果单元格值中有“时间表”,我想删除当前行,还有3个其他行:
在您的情况下,我相信它会起作用,如果
以下块:
您更改了最后一点:
希望它像对我一样有帮助!
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:
In your case, I believe it would work if:
In the following block:
You changed the last bit to:
Hope it helps just like it helped me!