Guest User

Untitled

a guest
Jun 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. >>> sys.getdefaultencoding()
  2. 'ascii'
  3. >>> sys.getfilesystemencoding()
  4. 'mbcs'
  5.  
  6. def setencoding():
  7. """Set the string encoding used by the Unicode implementation. The
  8. default is 'ascii', but if you're willing to experiment, you can
  9. change this."""
  10. encoding = "ascii" # Default value set by _PyUnicode_Init()
  11. if 0:
  12. # Enable to support locale aware default string encodings.
  13. import locale
  14. loc = locale.getdefaultlocale()
  15. if loc[1]:
  16. encoding = loc[1]
  17. if 0:
  18. # Enable to switch off string to Unicode coercion and implicit
  19. # Unicode to string conversion.
  20. encoding = "undefined"
  21. if encoding != "ascii":
  22. # On Non-Unicode builds this will raise an AttributeError...
  23. sys.setdefaultencoding(encoding) # Needs Python Unicode build !
  24.  
  25. >>> sys.getdefaultencoding()
  26. 'utf-8'
  27.  
  28. from BeautifulSoup import BeautifulStoneSoup
  29.  
  30. soup = BeautifulStoneSoup(urllib2.urlopen(URL),
  31. convertEntities=BeautifulStoneSoup.ALL_ENTITIES)
  32.  
  33. import sys
  34. sys.setdefaultencoding('utf-8')
Add Comment
Please, Sign In to add comment