Writing unicode strings via sys.stdout in Python

后端 未结 5 877
悲哀的现实
悲哀的现实 2020-11-30 02:22

Assume for a moment that one cannot use print (and thus enjoy the benefit of automatic encoding detection). So that leaves us with sys.stdout. Howe

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 02:37

    This is what I am doing in my application:

    sys.stdout.write(s.encode('utf-8'))

    This is the exact opposite fix for reading UTF-8 names from argv:

    for file in sys.argv[1:]:
        file = file.decode('utf-8')
    

    This is very ugly (IMHO) as it force you to work with UTF-8.. which is the norm on Linux/Mac, but not on windows... Works for me anyway :)

提交回复
热议问题