gpt4 book ai didi

python - 为什么我在这段代码中得到 "TypeError: unorderable types: str() < int()"?

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:37 25 4
gpt4 key购买 nike

@commands.command(pass_context=True)
@checks.serverowner()
async def change(self, ctx, change):
channel = self.bot.get_channel('432738903221469186')
change = ctx.message.clean_content[8:]
now = datetime.now()
count = len(self.changes.items()) + 1
self.changes[count] = {'date': now.strftime("%Y-%m-%d %H:%M:%S"), 'change': change}
dataIO.save_json('data/local/changes.json', self.changes)
await self.bot.send_message(channel, bold("\N{SPARKLE} This change to the server was just made:") + box(change))
await self.bot.add_reaction(ctx.message, "\N{WHITE HEAVY CHECK MARK}")

这里是 changes.json:

{
"1" : {
"change" : "TEST",
"date" : "2018-06-29 01:07:37"
}
}

这是完整的错误:

2|cdb_laun | Traceback (most recent call last):
2|cdb_laun | File "lib/discord/ext/commands/core.py", line 50, in wrapped
2|cdb_laun | ret = yield from coro(*args, **kwargs)
2|cdb_laun | File "/root/craig/cdbot/cogs/local.py", line 73, in change
2|cdb_laun | dataIO.save_json('data/local/changes.json', self.changes)
2|cdb_laun | File "/root/craig/cdbot/cogs/utils/dataIO.py", line 20, in save_json
2|cdb_laun | self._save_json(tmp_file, data)
2|cdb_laun | File "/root/craig/cdbot/cogs/utils/dataIO.py", line 50, in _save_json
2|cdb_laun | json.dump(data, f, indent=4, sort_keys=True, separators=(",", " : "))
2|cdb_laun | File "/usr/lib64/python3.5/json/__init__.py", line 178, in dump
2|cdb_laun | for chunk in iterable:
2|cdb_laun | File "/usr/lib64/python3.5/json/encoder.py", line 429, in _iterencode
2|cdb_laun | yield from _iterencode_dict(o, _current_indent_level)
2|cdb_laun | File "/usr/lib64/python3.5/json/encoder.py", line 352, in _iterencode_dict
2|cdb_laun | items = sorted(dct.items(), key=lambda kv: kv[0])
2|cdb_laun | TypeError: unorderable types: str() < int()

对于模糊的标题和问题,我深表歉意,我刚刚经常处理 JSON,我不理解这里的错误。我什至没有看到比较字符串和整数的位置。

我最初的想法是它与 count = len(self.changes.items()) + 1 有关,但事实并非如此。

最佳答案

对不起,我想通了。出于某种原因,在第一次运行期间,它将 JSON key 保存为字符串。在第二次运行中,它把它保存为一个整数。 self.changes 打印为:

{'2': {'date': '2018-06-29 02:02:03', 'change': 'TEST'}, '1': {'date': '2018-06-29 01:07:37', 'change': 'TEST'}}

为了解决这个问题,我只是改变了

self.changes[count] = {'date': now.strftime("%Y-%m-%d %H:%M:%S"), 'change': change}

self.changes[str(count)] = {'date': now.strftime("%Y-%m-%d %H:%M:%S"), 'change': change}

关于python - 为什么我在这段代码中得到 "TypeError: unorderable types: str() < int()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51093268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com