Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. >>> f = open('foo', 'w') # open file for writing in text mode
  2. >>> f.encoding
  3. 'UTF-8' # encoding is from the environment
  4. >>> f.write('€') # write a Unicode string
  5. 1
  6. >>> f.close()
  7. >>> exit()
  8. user@host:~$ hd foo
  9. 00000000 e2 82 ac |...| # data is UTF-8 encoded
  10.  
  11. >>> sys.stdout.encoding
  12. 'UTF-8' # encoding is from the environment
  13. >>> exit()
  14. user@host:~$ python3 -c 'print("€")' > foo
  15. user@host:~$ hd foo
  16. 00000000 e2 82 ac 0a |....| # data is UTF-8 encoded; n is from print()
  17.  
  18. user@host:~$ python3 -c 'print(b"xe2xf82xac")' > foo
  19. user@host:~$ hd foo
  20. 00000000 62 27 5c 78 65 32 5c 78 66 38 32 5c 78 61 63 27 |b'xe2xf82xac'|
  21. 00000010 0a |.|
  22.  
  23. #!/usr/bin/env python3
  24. import sys
  25. print('Content-Type: text/html; charset=utf-8')
  26. print()
  27. print('<html><body><pre>' + sys.stdout.encoding + '</pre>h€lló wörld<body></html>')
  28.  
  29. import locale # Ensures that subsequent open()s
  30. locale.getpreferredencoding = lambda: 'UTF-8' # are UTF-8 encoded.
  31.  
  32. import sys
  33. sys.stdin = open('/dev/stdin', 'r') # Re-open standard files in UTF-8
  34. sys.stdout = open('/dev/stdout', 'w') # mode.
  35. sys.stderr = open('/dev/stderr', 'w')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement