Python, how to print Japanese, Korean, Chinese strings

前端 未结 5 1879
情书的邮戳
情书的邮戳 2021-01-13 12:55

In Python, for Japanese, Chinese, and Korean,Python can not print the correct strings, for example hello in Japanese, Korean and Chinese are:

こん         


        
5条回答
  •  攒了一身酷
    2021-01-13 13:33

    First you need to read the text as unicode

    import codecs
    f = codecs.open('test.txt','r','utf-8')
    

    Second

    When you print you should encode it like this

    unicodeText.encode('utf-8')
    

    Third

    you should insure that your console support unicode display

    Use

    print sys.getdefaultencoding()
    

    if it doesn't try

    reload(sys)
    sys.setdefaultencoding('utf-8')
    

提交回复
热议问题