我已经写了下面的代码:Python“Re”模块对特殊语言敏感吗?
def process_file_step(message):
chat_id = message.chat.id
search = message.text
pattern = re.compile(u'.*%s.*\.pdf' %re.escape(search), re.I)
if next(filter(pattern.search, os.listdir('Files')), False):
bot.send_chat_action(chat_id, 'typing')
bot.send_message(chat_id, 'فایل هایی که نیاز داشتید :')
for files in filter(pattern.search, os.listdir('Files')):
requested_file = open('Files/' + files, 'rb')
bot.send_chat_action(chat_id, 'upload_document')
bot.send_document(chat_id, requested_file, caption='@RavanPediaBot')
requested_file.close()
else:
bot.send_chat_action(chat_id, 'typing')
bot.send_message(chat_id, 'چنین سندی وجود ندارد !')
bot.register_next_step_handler(bot.send_message(chat_id, 'درخواست دیگری دارید ؟', reply_markup=process_request_step_markup), process_request_step)
这从用户获取和搜索类似的名字,以它在文件中的字符串,并上传用户。它工作的很好,但问题是它不适用于波斯语,它总是发送一条消息,指出该文件不存在。当我在计算机上运行脚本时,它也适用于波斯语名称,但是当我使用codeanywhere.com时为了运行脚本,它不起作用。我使用pyTelegramBotAPI。另外我使用Python 3.x.我从函数参数中获取文本。我也用&转义搜索字符串,但没有奏效。
我印刷波斯消息,触发此错误:
UnicodeEncodeError: 'ASCII' 编解码器在0-3位置无法编码的字符:在范围序数不(128)
我将首先转义搜索模式:''。*%s。* \。pdf'%re.escape(search)'。从现在开始,可以在其中编写正则表达式。 –
查看标准库的pathlib模块中的新Path类。你可以很容易地做glob匹配:'Path(“Files”)。glob(“* {} *。pdf”.format(search))' – PaulMcG
@WillemVanOnsem谢谢,但它没有工作... –