unicode and encoding for persian or arabic in python3

后端 未结 2 1703
轮回少年
轮回少年 2020-12-21 21:13

some chunk of code like this:

city_name = obj[\'city_from\'][\'name\'].encode(\'utf-8\')
            print(city_name)

The output from this

相关标签:
2条回答
  • 2020-12-21 21:49

    okey i found my solution and it is working like a charm

    import sys
    sys.stdout.buffer.write(TestText2)
    

    UPDATE: this problem for ZSH script environment, i use bash and everything is find.

    0 讨论(0)
  • 2020-12-21 21:50

    Your answer shows that your terminal accepts utf-8 byte sequences.

    You don't need to convert Unicode string into bytes before printing them. Python does it for you.

    To change the character encoding that Python uses for I/O; set PYTHONIOENCODING=utf-8 environment variable or change your locale settings.

    It looks like sys.stdout.encoding is ascii in your case.

    $ python3 -c'import sys; print(sys.stdout.encoding)' 
    UTF-8
    $ python3 -c'import sys; print(sys.stdout.encoding)' | cat
    ascii
    $ LC_CTYPE=C python3 -c'import sys; print(sys.stdout.encoding)' 
    ANSI_X3.4-1968
    

    ANSI_X3.4-1968 is a canonical name for ascii.

    $ PYTHONIOENCODING=uTf-8 python3 -c'import sys; print(sys.stdout.encoding)' | cat
    uTf-8
    $ LC_CTYPE=C.UTF-8 python3 -c'import sys; print(sys.stdout.encoding)' 
    UTF-8
    

    Don't hardcode the character encoding inside your scripts. Print Unicode strings and configure your environment appropriately instead

    0 讨论(0)
提交回复
热议问题