reelpasob.blogg.se

Change text encoding python to
Change text encoding python to









  1. #Change text encoding python to software
  2. #Change text encoding python to code
  3. #Change text encoding python to windows

#Change text encoding python to windows

I didn't like the idea of forcing a sys reload and I couldn't get the system to cooperate with setting environment variables like PYTHONIOENCODING (tried direct Windows environment variable and also dropping that in a sitecustomize.py in site-packages as a one liner ='utf-8').

#Change text encoding python to software

This is a quick hack for anyone who is (1) On a Windows platform (2) running Python 2.7 and (3) annoyed because a nice piece of software (i.e., not written by you so not immediately a candidate for encode/decode printing maneuvers) won't display the "pretty unicode characters" in the IDLE environment (Pythonwin prints unicode fine), For example, the neat First Order Logic symbols that Stephan Boyer uses in the output from his pedagogic prover at First Order Logic Prover. Note: plains strings then are implicitely converted from utf-8 to unicode in SmartStdout before being converted to the output stream enconding. This way string literals and most operations (except character iteration) work comfortable without thinking about unicode conversion as if there would be Python3 only.įile I/O of course always need special care regarding encodings - as it is in Python3. _sys_org = imp.load_dynamic('_sys_org', 'sys') ĭef set_defaultencoding_globally(encoding='utf-8'):Īssert sys.getdefaultencoding() in ('ascii', 'mbcs', encoding) Change or drop libraries which still rely in a very dumb way fatally on ascii default encoding errors beyond chr #127 (which is rare today).Īnd do like this at application start (and/or via sitecustomize.py) in addition to the SmartStdout scheme above - without using reload(sys). For that, prefer " # encoding: utf-8" or ascii (no declaration).

#Change text encoding python to code

This can be done rather consistently (despite what anonbadger's article says) by taking care of a Python 2 or Python 2 + 3 source code basis which uses ascii or UTF-8 plain string literals consistently - as far as those strings potentially undergo silent unicode conversion and move between modules or potentially go to stdout. The only good reason to change the global default encoding (to UTF-8 only) I think is regarding an application source code decision - and not because of I/O stream encodings issues: For writing beyond-ascii string literals into code without being forced to always use u'string' style unicode escaping. Using beyond-ascii plain string literals in Python 2 / 2 + 3 code _stdout.write(s.encode(self.encoding, 'backslashreplace')) Getattr(org_stdout, 'encoding', None) or 'utf-8' Org_stdout = getattr(sys.stdout, 'org_stdout', sys.stdout) When the terminal / stream still cannot encode all occurring unicode chars, and when you don't want to break print's just because of that, you can introduce an encode-with-replace behavior in the translating file-like object.ĭef _init_(self, encoding=None, org_stdout=None): At last by replacing sys.stdout & sys.stderr by a translating file-like object. When is None for some reason, or non-existing, or erroneously false or "less" than what the stdout terminal or stream really is capable of, then try to provide a correct. from literals) on sys.stdout is: to take care of a sys.stdout (file-like object) which is capable and optionally tolerant regarding the needs: The best solution I know for solving the encode problem of print'ing unicode strings and beyond-ascii str's (e.g. sys.stdin/stdout streams, sys.excepthook, etc. reload often changes things in sys which have been put in place depending on the environment - e.g. When I use virtualenvwrapper the file I edit is ~/.virtualenvs/venv-name/lib/python2.7/sitecustomize.py.Īnd when I use with python notebooks and conda, it is ~/anaconda2/lib/python2.7/sitecustomize.pyįirst: reload(sys) and setting some random default encoding just regarding the need of an output terminal stream is bad practice. So the solution is to append to file /usr/lib/python2.7/sitecustomize.py the code: import sys After evaluating that module, the setdefaultencoding function is removed from sys. I guess there would be solution to it, but still I think using the hack should not be the correct option.Īfter trying many options, the one that worked for me was using the same code in the sitecustomize.py, where that piece of code is meant to be. In my case, it come with a side-effect: I'm using ipython notebooks, and once I run the code the ´print´ function no longer works. It is discouraged to use it (check this or this) Regarding python2 (and python2 only), some of the former answers rely on using the following hack: import sys











Change text encoding python to