I don't know why I'm getting "TypeError: '<' not supported between instances of 'str' and 'int'" error

I don't know why I'm getting "TypeError: '<' not supported between instances of 'str' and 'int'" error

我正在尝试创建一个跟踪器程序并使用 BeautifulSoup 并请求图书馆从 Steam 网站上获取价格。然后使用 openpyxl.

将这些价格值写入 excel 文件

前四款游戏的一切都完美无缺。当我尝试向列表中添加更多游戏时出现错误。

见下方代码

将空心骑士项目添加到 wistlist 数组后,我开始收到此错误:

TypeError: 'str' 和 'int'

实例之间不支持“<”

即使我没有使用任何比较符号来比较此程序中的值。

旁注: 我有 json 文件存储了将值放入 excel 的下一行。在执行完上面的所有代码后,程序会将行值更新为 1。请参阅下面的函数 updateTrackingLines()

cookies = {'birthtime': '568022401'} # For Steam's age verification

wishlist = [
    "https://store.steampowered.com/app/477160/Human_Fall_Flat/",
    "https://store.steampowered.com/app/242760/The_Forest/",
    "https://store.steampowered.com/app/4000/Garrys_Mod/",
    "https://store.steampowered.com/app/271590/Grand_Theft_Auto_V/",
    "https://store.steampowered.com/app/367520/Hollow_Knight/",
    "https://store.steampowered.com/app/588650/Dead_Cells/s",
    "https://store.steampowered.com/app/320240/We_Happy_Few/", "https://store.steampowered.com/app/589510/Shovel_Knight_Specter_of_Torment/",
    "https://store.steampowered.com/app/413150/Stardew_Valley/"
]
l = len(wishlist)
def updateProducts():
    wb = openpyxl.load_workbook("C:\Python37\SteamTracker\ProductsTracker.xlsx")
    sheets = wb.get_sheet_names()
    today = date.today()
    today = today.strftime("%m/%d/%Y")
    lines = getTrackingLines()
    for i in range(0,l):
        sheet = wb.get_sheet_by_name(sheets[i])
        html = requests.get(wishlist[i],cookies=cookies)
        page_soup = soup(html.content, "html.parser")
        price = page_soup.find("div",{"class":"game_purchase_price price"}).text.strip()
        price = price[1:len(price)]
        sheet.cell(lines[i],2, value = float(price)) # updating cells
        sheet.cell(lines[i],1, value = today)
        print("successed updating " + sheet.title)
    wb.save("C:\Python37\SteamTracker\ProductsTracker.xlsx")
    updateTrackingLines() # See explaination of this func above

完整追溯

PS C:\Python37\SteamTracker> & C:/Users/Han/AppData/Local/Programs/Python/Python37/python.exe c:/Python37/SteamTracker/main.py
c:/Python37/SteamTracker/main.py:48: DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
  sheets = wb.get_sheet_names()
c:/Python37/SteamTracker/main.py:53: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
  sheet = wb.get_sheet_by_name(sheets[i])
successed updating Human Fall Flat
successed updating The Forest
successed updating Garry's Mod
successed updating GTA V
Traceback (most recent call last):
  File "c:/Python37/SteamTracker/main.py", line 75, in <module>
    updateProducts()
  File "c:/Python37/SteamTracker/main.py", line 58, in updateProducts
    sheet.cell(lines[i],2, value = float(price)) # updating cells
  File "C:\Users\Han\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\worksheet\worksheet.py", line 236, in cell
    if row < 1 or column < 1:
TypeError: '<' not supported between instances of 'str' and 'int'

如错误所述,< 用于比较 strint

  File "C:\Users\Han\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\worksheet\worksheet.py", line 236, in cell
    if row < 1 or column < 1:
TypeError: '<' not supported between instances of 'str' and 'int'

来自代码行

    if row < 1 or column < 1:

您似乎通过了 str 作为 rowcolumn。在查看您的代码时,lines[i] 可能是 str.

  File "c:/Python37/SteamTracker/main.py", line 58, in updateProducts
    sheet.cell(lines[i],2, value = float(price)) # updating cells

问题解决了!

看来我在这里用 json 文件制造了一个令人生畏的小错误。

当我在我的 json 上添加额外的项目时,我不小心将值行编码为 str 类型,而不是简单的 int。

并且我们需要一个int值来定位excelsheet中的单元格,但是我的代码值行是str类型。

我最好学会注意错别字。

感谢您对本主题的帮助和参与

{
  "titles": [
    {
      "name": "Human Fall Flat",
      "line": 6
    },
    {
      "name": "The Forest",
      "line": 6
    },
    {
      "name": "Garry's Mod",
      "line": 6
    },
    {
      "name": "Grand Theft Auto V",
      "line": 6
    },
    {
      "name": "Hollow Knight",
      "line": "2"
    },
    {
      "name": "Dead Cells",
      "line": "2"
    },
    {
      "name": "We Happy Few",
      "line": "2"
    },
    {
      "name": "Shovel Knight: Specter of Torment",
      "line": "2"
    },
    {
      "name": "Stardew Valley",
      "line": "2"
    }
  ]
}