TypeError: getattr(): attribute name must be string
TypeError: getattr(): attribute name must be string
我需要将输出组织为电报机器人中的按钮。例如,我创建了一个字典,我需要从中推断键和对象,将其形成为字符串并将其放置在将通过 for 循环的按钮中。但是我看到的不是正确的结果,而是这个错误:TypeError: getattr(): attribute name must be string。
这是我的代码:
def choose_language(update, conext):
chat = update.effective_chat
land = {
'Eng':'en',
'Rus':'ru'
}
b = []
for k,v in land.items():
a = k, v
b.append(a)
flatten = [str(item) for sub in b for item in sub]
for key in flatten:
button = [InlineKeyboardButton(f"{key}", callback_data='{key}')]
#for key in land:
#button = InlineKeyboardButton(str(land[key]).capitalize(), callback_data=str(key))
reply_markup = InlineKeyboardMarkup(button)
update.message.reply_text('Choose target language:', reply_markup=reply_markup)
InlineKeyboardMarkup
的参数必须是列表的列表 - 您传递的是一个简单的列表。
所以我做到了。问题是当它引发 AttributeError: the list object has no to_dict attribute
时,我将按钮制作为列表。然后我放弃了 dict.attributes 并决定缩短 flatten
以仅获取字符串对象。
d = {
'Eng':'en',
'Rus':'ru',
'Ger':'ge'
}
button_demo = []
b = []
bb = []
for k, v in d.items():
a = k
b.append(a)
s = v
bb.append(s)
flatten = [str(sub) for sub in b]
flatten1 = [str(sub) for sub in bb]
for ix in range(len(flatten)):
button = [InlineKeyboardButton(f"{flatten[ix]}", callback_data=f'{flatten1[ix]}')]
button_demo.append(button)
reply_markup = InlineKeyboardMarkup(button_demo)
update.message.reply_text('Choose target language:', reply_markup=reply_markup)
我需要将输出组织为电报机器人中的按钮。例如,我创建了一个字典,我需要从中推断键和对象,将其形成为字符串并将其放置在将通过 for 循环的按钮中。但是我看到的不是正确的结果,而是这个错误:TypeError: getattr(): attribute name must be string。 这是我的代码:
def choose_language(update, conext):
chat = update.effective_chat
land = {
'Eng':'en',
'Rus':'ru'
}
b = []
for k,v in land.items():
a = k, v
b.append(a)
flatten = [str(item) for sub in b for item in sub]
for key in flatten:
button = [InlineKeyboardButton(f"{key}", callback_data='{key}')]
#for key in land:
#button = InlineKeyboardButton(str(land[key]).capitalize(), callback_data=str(key))
reply_markup = InlineKeyboardMarkup(button)
update.message.reply_text('Choose target language:', reply_markup=reply_markup)
InlineKeyboardMarkup
的参数必须是列表的列表 - 您传递的是一个简单的列表。
所以我做到了。问题是当它引发 AttributeError: the list object has no to_dict attribute
时,我将按钮制作为列表。然后我放弃了 dict.attributes 并决定缩短 flatten
以仅获取字符串对象。
d = {
'Eng':'en',
'Rus':'ru',
'Ger':'ge'
}
button_demo = []
b = []
bb = []
for k, v in d.items():
a = k
b.append(a)
s = v
bb.append(s)
flatten = [str(sub) for sub in b]
flatten1 = [str(sub) for sub in bb]
for ix in range(len(flatten)):
button = [InlineKeyboardButton(f"{flatten[ix]}", callback_data=f'{flatten1[ix]}')]
button_demo.append(button)
reply_markup = InlineKeyboardMarkup(button_demo)
update.message.reply_text('Choose target language:', reply_markup=reply_markup)