首页 > 解决方案 > 初学者 - Python .sort() 中的 TypeError

问题描述

我将通过从 Python 中的 Excel 文件中读取数据来对数据进行排序。

但是,我遇到了一些错误,下面是我的代码。

 mp_list = []
 for i in range(2, maxRow_input+1):
    mp=sheet_input.cell(i,mp_index).value
    mp_list.append(mp)
 mp_list.sort()
 print(mp_list)

这就是我收到的错误消息:

 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 <ipython-input-110-9af441d1bedd> in <module>
      3     mp=sheet_input.cell(i,mp_index).value
      4     mp_list.append(mp)
  ----> 5 mp_list.sort()
      6 print(mp_list)

 TypeError: '<' not supported between instances of 'str' and 'int'

谁能帮我解决这个问题,请给我解释一下:)

标签: pythonexceltypeerror

解决方案


请检查该 excel 列中的值是否具有相同的数据类型。似乎该列具有不同的数据类型,因此排序给出了错误。


推荐阅读