OVH Cloud OVH Cloud

Type Unicode to ANSI

2 réponses
Avatar
alain.cartron
Salut a tous,
j'ai SQL Server qui g=E9n=E8re par defaut des fichiers texte cod=E9s
Unicode. Quand je les lit avec python (exec du fichier .py) j'ai une
ligne qui ressemble a :

LIGNE : =A6J o b ' B a c k u p S T C C l o t u r e ' : S t e
p 1 , ' b a c k u p S T C C l o t u r e e P l a n n i n g
: B e g a n E x e c u t i n g 2 0 0 5 - 0 9 - 1 4 1 2 : 0 1 :
4 9

au lieu de (si fichier ANSI) :
Job 'Backup STC Cloture' : Step 1, 'backup STC Cloture ePlanning' :
Began Executing 2005-09-14 12:01:49

Donc je ne peux pas trouver la chaine caract=E8re que je veux (d'autres
SQL Server g=E9n=E8re bien du ANSI et la recherche de chaine est OK)

On m'a conseill=E9 d'utiliser pour convertir les Unicode :

import locale
ligne.encode( locale.getpreferredencoding(), 'replace')

Mais si je lit le meme fichier .txt en GUI python j'ai ces caract=E8res
la :

ligne :
"\xff\xfeJ\x00o\x00b\x00 \x00'\x00B\x00a\x00c\x00k\x00u\x00p\x00
\x00S\x00T\x00C\x00 \x00C\x00l\x00 etc...
Donc si je mets la ligne de code pr=E9c=E9dente dans mon pythoon j'ai un
retour comme :

File "E:\PYTHON23\lib\encodings\cp1252.py", line 18, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0:
ordinal not in range(128)

Comment puis-je faire ? Merci.

2 réponses

Avatar
Yermat
Salut a tous,
j'ai SQL Server qui génère par defaut des fichiers texte codés
Unicode. [...]
On m'a conseillé d'utiliser pour convertir les Unicode :
import locale
ligne.encode( locale.getpreferredencoding(), 'replace')
[...]


Mauvais conseil :-D

Va voir http://docs.python.org/lib/module-codecs.html
Et utilise codecs.open au lieu de open ou file.

Ensuite quel est l'encodage de Unicode utilisé ? UTF-8 ? UTF-16 ?

import codecs
monfichier = codecs.open(filename, "r", "utf_8")
for line in monfichier:
print monfichier

Ensuite effectivement si tu affiches en console du texte tu dois soit
réencoder soit modifier ton fichier sitecustomize.py

Lire l'article suivant pourra vous aider à mieux comprendre l'unicode :
http://french.joelonsoftware.com/
http://www.joelonsoftware.com/articles/Unicode.html

Sinon cherchez dans google python et unicode...
http://www.reportlab.com/i18n/python_unicode_tutorial.html
http://diveintopython.org/xml_processing/unicode.html

--
Yermat

Avatar
Laurent Pointal
Yermat wrote:

Salut a tous,
j'ai SQL Server qui génère par defaut des fichiers texte codés
Unicode. [...]
On m'a conseillé d'utiliser pour convertir les Unicode :
import locale
ligne.encode( locale.getpreferredencoding(), 'replace')

[...]


Mauvais conseil :-D

Va voir http://docs.python.org/lib/module-codecs.html
Et utilise codecs.open au lieu de open ou file.

Ensuite quel est l'encodage de Unicode utilisé ? UTF-8 ? UTF-16 ?

import codecs
monfichier = codecs.open(filename, "r", "utf_8")
for line in monfichier:
print monfichier

Ensuite effectivement si tu affiches en console du texte tu dois soit
réencoder soit modifier ton fichier sitecustomize.py

Lire l'article suivant pourra vous aider à mieux comprendre l'unicode :
http://french.joelonsoftware.com/
http://www.joelonsoftware.com/articles/Unicode.html

Sinon cherchez dans google python et unicode...
http://www.reportlab.com/i18n/python_unicode_tutorial.html
http://diveintopython.org/xml_processing/unicode.html



J'ajouterais la page du Wiki python-fr qui y est consacrée:
http://wikipython.flibuste.net/moin.py/JouerAvecUnicode