Encoding problem when launching Python27 via DOS
Jean-Pierre M
pythonrubylang at gmail.com
Sun Apr 10 08:22:18 EDT 2011
I created a simple program which writes in a unicode files some french text
with accents!
*# -*- coding: cp1252 -*-*
*#!/usr/bin/python*
*'''*
*Created on 27 déc. 2010*
*
*
*@author: jpmena*
*'''*
*from datetime import datetime*
*import locale*
*import codecs*
*import os,sys*
*
*
*class Log(object):*
* log=None*
* def __init__(self,log_path,charset_log=None):*
* self.log_path=log_path*
* if(os.path.exists(self.log_path)):*
* os.remove(self.log_path)*
* #self.log=open(self.log_path,'a')*
* if charset_log is None:*
* self.charset_log=sys.getdefaultencoding()*
* else:*
* self.charset_log=charset_log*
* self.log=codecs.open(self.log_path, "a", charset_log)*
* *
* def getInstance(log_path=None):*
* print "encodage systeme:"+sys.getdefaultencoding()*
* if Log.log is None:*
* if log_path is None:*
* log_path=os.path.join(os.getcwd(),'logParDefaut.log')*
* Log.log=Log(log_path)*
* return Log.log*
* *
* getInstance=staticmethod(getInstance)*
* *
* def p(self,msg):*
* aujour_dhui=datetime.now()*
* date_stamp=aujour_dhui.strftime("%d/%m/%y-%H:%M:%S")*
* print sys.getdefaultencoding()*
* unicode_str=u'%s : %s \n' %
(date_stamp,msg.encode(self.charset_log,'replace'))*
* self.log.write(unicode_str)*
* return unicode_str*
* *
* def close(self):*
* self.log.flush()*
* self.log.close()*
* return self.log_path*
*
*
*if __name__ == '__main__':*
* l=Log.getInstance()*
* l.p("premier message de Log à accents")*
* Log.getInstance().p("second message de Log")*
* l.close()*
I am using PyDev/Aptana for developping. Il Aptana lanches the program
everything goes well!!! sys.getdefaultencoding() answers 'cp1252'
But if I execute the following batch file in a DOS console on my Windows
VISTA:
*@echo off*
*setlocal*
*chcp 1252*
*set PYTHON_HOME=C:\Python27*
*for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (*
* if "%%l"=="" (*
* :: Windows XP*
* set D=%%k%%j%%i*
* ) else (*
* :: Windows NT/2000*
* set D=%%l%%k%%j*
* )*
*)*
*set PYTHONIOENCODING=cp1252:backslashreplace*
*%PYTHON_HOME%\python.exe "%~dp0\src\utils\Log.py"*
the answer is:
*C:\Users\jpmena\Documents\My
Dropbox\RIF\Python\VelocityTransforms>generationPro*
*grammeSitePublicActuel.cmd*
*Page de codes active : 1252*
*encodage systeme:ascii*
*ascii*
*Traceback (most recent call last):*
* File "C:\Users\jpmena\Documents\My
Dropbox\RIF\Python\VelocityTransforms\\src\*
*utils\Log.py", line 51, in <module>*
* l.p("premier message de Log à accents")*
* File "C:\Users\jpmena\Documents\My
Dropbox\RIF\Python\VelocityTransforms\\src\*
*utils\Log.py", line 40, in p*
* unicode_str=u'%s : %s \n' %
(date_stamp,msg.encode(self.charset_log,'replac*
*e'))*
*UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 23:
ordinal*
* not in range(128)*
sys.getdefaultencoding answers ascii so the encode function cannot encode
the accent in 'à'
I am using Python27 because it is compatible with the actual versions of
pyodbc (for accessinf a ACCESS database) and airspeed (Velocity Templates in
utf-8)
The target is to launch airspeed applications via the Windows CRON
Can someone help me. I am really stuck!
Thanks...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110410/28f99055/attachment.html>
More information about the Python-list
mailing list