Multilingual care about Python code?

Say I had the following code -

homeDir = os.path.expanduser("~")
fullPath = homeDir + "/.config"
print fullPath

Will this code still function properly for someone, say, in Japan, in which home directory was made up of kanji?

My concern is that python does not know how to add two languages ​​together, or even know what to do with foreign characters.

+4
source share
2 answers

All lines in your code from the question are bytes (a sequence of bytes). They can represent anything, including text encoded in some character encoding.

homeDir = os.path.expanduser("~") # input bytestring, returns bytestring
fullPath = homeDir + "/.config" # add 2 bytestrings 
print fullPath

print, , . , .


Python 3 from __future__ import unicode_literals, - Unicode. :

from __future__ import unicode_literals

homeDir = os.path.expanduser("~") # input Unicode, returns Unicode
fullPath = homeDir + "/.config" # add 2 Unicode strings
print(fullPath) # print Unicode

( PYTHONIOENCODING ).

POSIX ( ), , . Python 3 docs:

Python , . , . Python (. sys.getfilesystemencoding()).

3.1: . Python surrogateescape , , unecodable Unicode U + DCxx .

, fullPath U+DCxx, print(fullPath) , . os.fsencode(fullPath) , .

+3

, , , .

+2

All Articles