1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python cyrillic string encoding

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I try to convert cyrillic string to readable format. I have the similar code for php and it work fine. But python was harder.

    import re
    from sys import getdefaultencoding

    def decodeString(matches):
    return chr(int(matches.group(0).lstrip('\\'), 8))

    my_string = r'\320\222\321\213\320\263\321\200\321\203\320\267\320\272\320\260 \320\267\320\260\321\217\320\262\320\276\320\272'
    decoded_string = re.sub(r'\\[0-7]{3}', decodeString, my_string)
    print(decoded_string)

    my_string2 = "\320\222\321\213\320\263\321\200\321\203\320\267\320\272\320\260 \320\267\320\260\321\217\320\262\320\276\320\272"
    print(my_string2)

    print(sys.stdin.encoding, sys.stdout.encoding)


    Both these way return the same result:

    ÐÑгÑÑзка заÑвок в 1C
    ÐÑгÑÑзка заÑвок в 1C
    utf-8 utf-8


    Expected result - "Выгрузка заявок".

    Continue reading...

Compartilhe esta Página