Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7: reload(sys) disables error messages and print in Windows

I'm making a script that requires me to change the encoding format to "UTF-8". I found a topic here on Stachoverflow that said i could use:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

It works great in OSX 10.8 (maybe earlier versions too), but in Windows XP and Windows 7 (probably Vista and 8 too) it disables all feedback in the interpreter. The script still runs, but i can't print anything or see if anything goes wrong.

Is there a way to patch the current code or is there an alternate way to change the encoding?

like image 742
Mads Y Avatar asked Nov 03 '12 14:11

Mads Y


2 Answers

May be what happen to you are related with idle, since idle replace default sys.stdin, sys.stdout, sys.stderr with its own object. After you reload(sys), the three file object associated with sys will be restored to default ones, so you can not see it in idle.

You may solve it by change them back after reload(sys):

import sys
stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
reload(sys)
sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
like image 192
socrates Avatar answered Nov 18 '22 20:11

socrates


To be frank, I have zero idea why you would possibly want to alter the default encoding for Python just to read and parse a single file (or even a great number of files, for that matter). Python can quite easily parse and handle UTF-8 without such drastic measures. Moreover, on this very site, there are some great methods to do so. This issue is close to a duplicate of: Unicode (UTF-8) reading and writing to files in Python

On that line, the best answer is: https://stackoverflow.com/a/844443/678533, which basically relies on the Python Codecs module.

Using this approach, you can do the following:

import codecs
with codecs.open("SomeFile", "rb", "utf-8") as inFile: 
    text = inFile.read()
# Do something with 'text' here
with codecs.open("DifferentFile", "wb", "utf-8") as outFile:
    outFile.write(text)

This successfully reads a UTF-8 formatted file, then writes it back out as UTF-8. The variable 'text' will be a unicode string in Python. You can always write it back out as UTF-8 or UTF-16 or any compatible output format.

like image 22
Namey Avatar answered Nov 18 '22 19:11

Namey