B
B
Brad_baf2020-05-25 17:07:01
Python
Brad_baf, 2020-05-25 17:07:01

How to fix encoding problem (UnicodeEncodeError: 'latin-1' codec can't encode characters..) in tg bot?

Bot that creates qrcode. If you send him text with a space between words in Russian, he will give an error (UnicodeEncodeError: 'latin-1' codec can't encode characters..). What to do?

def qrcodegen(message):
    def is_allowed(string):
        characherRegex = re.compile(
            r'[^\*\@\#\$\%\^\!\"\№\;\:\?\(\)\_\ \-\+\=\&\]\[\{\}\/\'\,a-zA-Zа-яА-Я0-9.]')
        string = characherRegex.search(string)
        return not bool(string)
    x = message.text
    if is_allowed(x):
        qr = pyqrcode.create(x)
        qr.png('your_qrcode.png', scale=8)
        qr = open('your_qrcode.png', 'rb')
        bot.send_message(message.chat.id, "Your qrcode:", parse_mode='html')
        bot.send_photo(message.chat.id, qr)
    else:
        bot.send_message(
            message.chat.id, "Something went wrong", parse_mode='html')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-05-25
@Brad_baf

qr = pyqrcode.create(x.encode('utf-8'))
And next time please provide the full text of the error. It's all there where the problem is. What the hell are you making guessing on the coffee grounds?
Do you think the interpreter just wrote all this "garbage" to you?
So they would have asked the question: I wrote a program - it doesn’t work, pmagiti!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question