Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> f = open('foo', 'w') # open file for writing in text mode
- >>> f.encoding
- 'UTF-8' # encoding is from the environment
- >>> f.write('€') # write a Unicode string
- 1
- >>> f.close()
- >>> exit()
- user@host:~$ hd foo
- 00000000 e2 82 ac |...| # data is UTF-8 encoded
- >>> sys.stdout.encoding
- 'UTF-8' # encoding is from the environment
- >>> exit()
- user@host:~$ python3 -c 'print("€")' > foo
- user@host:~$ hd foo
- 00000000 e2 82 ac 0a |....| # data is UTF-8 encoded; n is from print()
- user@host:~$ python3 -c 'print(b"xe2xf82xac")' > foo
- user@host:~$ hd foo
- 00000000 62 27 5c 78 65 32 5c 78 66 38 32 5c 78 61 63 27 |b'xe2xf82xac'|
- 00000010 0a |.|
- #!/usr/bin/env python3
- import sys
- print('Content-Type: text/html; charset=utf-8')
- print()
- print('<html><body><pre>' + sys.stdout.encoding + '</pre>h€lló wörld<body></html>')
- import locale # Ensures that subsequent open()s
- locale.getpreferredencoding = lambda: 'UTF-8' # are UTF-8 encoded.
- import sys
- sys.stdin = open('/dev/stdin', 'r') # Re-open standard files in UTF-8
- sys.stdout = open('/dev/stdout', 'w') # mode.
- sys.stderr = open('/dev/stderr', 'w')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement