python 3.0, how to make print() output unicode?

前端 未结 5 1680
猫巷女王i
猫巷女王i 2020-11-28 15:49

I\'m working in WinXP 5.1.2600, writing a Python application involving Chinese pinyin, which has involved me in endless Unicode problems. Switching to Python 3.0 has solved

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 16:10

    Here's a dirty hack:

    # works
    import os
    os.system("chcp 65001 &")
    print("юникод")
    

    However everything breaks it:

    • simple muting first line already breaks it:

      # doesn't work
      import os
      os.system("chcp 65001 >nul &")
      print("юникод")
      
    • checking for OS type breaks it:

      # doesn't work
      import os
      if os.name == "nt":
          os.system("chcp 65001 &")
      
      print("юникод")
      
    • it doesn't even works under if block:

      # doesn't work
      import os
      if os.name == "nt":
          os.system("chcp 65001 &")
          print("юникод")
      

    But one can print with cmd's echo:

    # works
    import os
    os.system("chcp 65001 & echo {0}".format("юникод"))
    

    and here's a simple way to make this cross-platform:

    # works
    
    import os
    
    def simple_cross_platrofm_print(obj):
        if os.name == "nt":
            os.system("chcp 65001 >nul & echo {0}".format(obj))
        else:
            print(obj)
    
    simple_cross_platrofm_print("юникод")
    

    but the window's echo trailing empty line can't be suppressed.

提交回复
热议问题