print UTF-8 character in Python 2.7

前端 未结 2 1730
陌清茗
陌清茗 2020-12-30 14:28

Here is how I open, read and output. The file is an UTF-8 encoded file for unicode characters. I want to print the first 10 UTF-8 characters, but the output from below code

2条回答
  •  醉梦人生
    2020-12-30 15:13

    To output a Unicode string to a file or to the console you need to choose a text encoding. In Python the default text encoding is ASCII, but to support other characters you need to use a different encoding, such as UTF-8:

    s = unicode(your_object).encode('utf8')
    print s
    

提交回复
热议问题