Chinese and Japanese character support in python

后端 未结 3 1724
执笔经年
执笔经年 2020-12-06 02:53

How to read correctly japanese and chinese characters. I\'m using python 2.5. Output is displayed as \"E:\\Test\\?????????\"

path = r\"E:\\Test\         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-06 03:16

    There are two independent issues:

    1. You should specify Python source encoding if you use non-ascii characters and use Unicode literals for data that represents text e.g.:

      # -*- coding: utf-8 -*-
      path = ur"E:\Test\は最高のプログラマ"
      
    2. Printing Unicode to Windows console is complicated but if you set correct font then just:

      print path
      

      might work.

    Regardless of whether your console can display the path; it should be fine to pass the Unicode path to filesystem functions e.g.:

    entries = os.listdir(path)
    

    Don't call .encode(char_enc) on bytestrings, call it on Unicode strings instead.
    Don't call .decode(char_enc) on Unicode strings, call it on bytestrings instead.

提交回复
热议问题