对于我的示例,我如何处理 python 中的 KeyError
How can I handle KeyError in python for my example
我正在使用 Python 和 Firebase 制作电报机器人。
我卡在了 KeyError:
2021-02-12 12:39:38,687 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\dispatcher.py", line 442, in process_update
handler.handle_update(update, self, check, context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\conversationhandler.py", line 549, in handle_update
new_state = handler.handle_update(update, dispatcher, check_result, context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\handler.py", line 160, in handle_update
return self.callback(update, context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\bot.py", line 61, in start
print(user.val()["chat_id"])
KeyError: 'chat_id'
这是我的代码:
def start(update: Update, context: CallbackContext) -> int:
global PauseKey, PauseStart, PauseState, PauseAllKey
timenow = datetime.now()
auth = firebase.auth()
userx = auth.sign_in_with_email_and_password('drux@mail.com', '12345678')
db = firebase.database()
chat_user_client = update.message.from_user.username
all_users = db.child("users").get()
user_status = "none"
for user in all_users.each():
print(user.val()["chat_id"])
if user.val()["chat_id"] == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
print(user_status)
if user_status == "exist":
text_back = "You was logged in, Welcome back "
else:
data = {"name": chat_user_client, "chat_id": update.message.chat_id}
date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
userKey= db.child("users").push(data)
userKey = userKey.val()
text_back = "User was create"
update.message.reply_text(
f'{text_back}{chat_user_client}',
reply_markup=markup2
)
return CHOOSING
错误块:
for user in all_users.each():
print(user.val()["chat_id"])
if user.val()["chat_id"] == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
并且此打印 print(user.val()["chat_id"])
在控制台中显示 690953230
。这是我的 firebase 中存储的正确聊天 ID。
我尝试将 await 与 asyncio 库一起使用,对于这一行:
all_users = db.child("users").get()
async def start()
...
all_user = await db.child("users").get()
...
使用 async 和 await 错误正在离开,但代码没有执行预期的操作。
代码应执行以下操作:
浏览所有用户数据并检查 user_chat_id 是否已在数据库中。
如果 chat_id 已经在数据库中,则用户存在并且我正在使用他的 table 密钥。如果用户不存在,我将创建他的 table 并获取他的用户密钥。
我被这个错误卡住了好几个小时,我自己无法处理,请帮忙。
Edit
现在我发现如果我的用户存在,代码就可以正常工作。
我的意思是,如果我的 chat_id 已经在数据库中,我不会收到任何错误,
如果我从数据库中删除 im 数据我得到 keyError
我建议你在从字典中获取元素时使用get;因为当 dict["key"] 给出错误时, dict.get("key") 在键不存在时不会给出错误。我的建议是将错误块更新为:
for user in all_users.each():
chat_id = user.val().get("chat_id")
if chat_id and chat_id == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
此外,您应该进行一些重构以增强可读性。例如,在上面的代码块中,以下赋值看起来是多余的:
userKey = user.key()
您还可以提取用户存在性检查作为另一种方法:
def user_exists(all_users, msg_chat_id):
for user in all_users.each():
chat_id = user.val().get("chat_id")
if chat_id and chat_id == msg_chat_id:
return true
return false
然后在启动方法中使用:
def start(update: Update, context: CallbackContext) -> int:
global PauseKey, PauseStart, PauseState, PauseAllKey
timenow = datetime.now()
auth = firebase.auth()
userx = auth.sign_in_with_email_and_password('drux@mail.com', '12345678')
db = firebase.database()
chat_user_client = update.message.from_user.username
all_users = db.child("users").get()
if user_exists(all_users, update.message.chat_id):
text_back = "You was logged in, Welcome back "
else:
data = {"name": chat_user_client, "chat_id": update.message.chat_id}
date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
userKey= db.child("users").push(data)
userKey = userKey.val()
text_back = "User was create"
update.message.reply_text(
f'{text_back}{chat_user_client}',
reply_markup=markup2
)
return CHOOSING
我正在使用 Python 和 Firebase 制作电报机器人。 我卡在了 KeyError:
2021-02-12 12:39:38,687 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\dispatcher.py", line 442, in process_update
handler.handle_update(update, self, check, context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\conversationhandler.py", line 549, in handle_update
new_state = handler.handle_update(update, dispatcher, check_result, context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\venv\lib\site-packages\telegram\ext\handler.py", line 160, in handle_update
return self.callback(update, context)
File "C:\Users\Lil-Dredd\PycharmProjects\TelegramBot\bot.py", line 61, in start
print(user.val()["chat_id"])
KeyError: 'chat_id'
这是我的代码:
def start(update: Update, context: CallbackContext) -> int:
global PauseKey, PauseStart, PauseState, PauseAllKey
timenow = datetime.now()
auth = firebase.auth()
userx = auth.sign_in_with_email_and_password('drux@mail.com', '12345678')
db = firebase.database()
chat_user_client = update.message.from_user.username
all_users = db.child("users").get()
user_status = "none"
for user in all_users.each():
print(user.val()["chat_id"])
if user.val()["chat_id"] == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
print(user_status)
if user_status == "exist":
text_back = "You was logged in, Welcome back "
else:
data = {"name": chat_user_client, "chat_id": update.message.chat_id}
date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
userKey= db.child("users").push(data)
userKey = userKey.val()
text_back = "User was create"
update.message.reply_text(
f'{text_back}{chat_user_client}',
reply_markup=markup2
)
return CHOOSING
错误块:
for user in all_users.each():
print(user.val()["chat_id"])
if user.val()["chat_id"] == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
并且此打印 print(user.val()["chat_id"])
在控制台中显示 690953230
。这是我的 firebase 中存储的正确聊天 ID。
我尝试将 await 与 asyncio 库一起使用,对于这一行:
all_users = db.child("users").get()
async def start()
...
all_user = await db.child("users").get()
...
使用 async 和 await 错误正在离开,但代码没有执行预期的操作。
代码应执行以下操作: 浏览所有用户数据并检查 user_chat_id 是否已在数据库中。 如果 chat_id 已经在数据库中,则用户存在并且我正在使用他的 table 密钥。如果用户不存在,我将创建他的 table 并获取他的用户密钥。
我被这个错误卡住了好几个小时,我自己无法处理,请帮忙。
Edit
现在我发现如果我的用户存在,代码就可以正常工作。 我的意思是,如果我的 chat_id 已经在数据库中,我不会收到任何错误, 如果我从数据库中删除 im 数据我得到 keyError
我建议你在从字典中获取元素时使用get;因为当 dict["key"] 给出错误时, dict.get("key") 在键不存在时不会给出错误。我的建议是将错误块更新为:
for user in all_users.each():
chat_id = user.val().get("chat_id")
if chat_id and chat_id == update.message.chat_id:
user_status = "exist"
userKey = user.key()
break
此外,您应该进行一些重构以增强可读性。例如,在上面的代码块中,以下赋值看起来是多余的:
userKey = user.key()
您还可以提取用户存在性检查作为另一种方法:
def user_exists(all_users, msg_chat_id):
for user in all_users.each():
chat_id = user.val().get("chat_id")
if chat_id and chat_id == msg_chat_id:
return true
return false
然后在启动方法中使用:
def start(update: Update, context: CallbackContext) -> int:
global PauseKey, PauseStart, PauseState, PauseAllKey
timenow = datetime.now()
auth = firebase.auth()
userx = auth.sign_in_with_email_and_password('drux@mail.com', '12345678')
db = firebase.database()
chat_user_client = update.message.from_user.username
all_users = db.child("users").get()
if user_exists(all_users, update.message.chat_id):
text_back = "You was logged in, Welcome back "
else:
data = {"name": chat_user_client, "chat_id": update.message.chat_id}
date = {"date": datetime.today().strftime('%Y-%m-%d-%H:%M:%S')}
userKey= db.child("users").push(data)
userKey = userKey.val()
text_back = "User was create"
update.message.reply_text(
f'{text_back}{chat_user_client}',
reply_markup=markup2
)
return CHOOSING