Note that there are some explanatory texts on larger screens.

plurals
  1. POArcPy and Python encoding messing up?
    text
    copied!<p>I am faced with a strange behavior between ArcPy and Python encoding. I work with VisualStudio 2010 Shell with Python tools for VS (PTVS) installed. I isolated my problem through a simple script file. The py script file that contains the following commands. In VisualStudio, I have set the « Advanced Save Options...» to « UTF-8 without signature ». The script simply print on the screen a accented string, then import arcpy module, then again print the same string. Importing Arcpy seems to change the Python encoding setup but I don't know why and I would like to restablish it correctly because it causes problems a little bit everywhere in the original script.</p> <hr> <p>I checked the python « encoding » folder and erased every pyc file. Than I ran the script and it generated 3 pyc files :<br></p> <ol> <li>cp850.pyc (which corresponds to my stdout.encoding)</li> <li>cp1252.pyc (which corresponds to my Windows environment encoding)</li> <li>utf_8.pyc (which fits the encoding of my script)</li> </ol> <p>When ArcPy is being imported, something comes altering the encoding that affects the initial variables. </p> <p>Why?</p> <p>Is it possible with some Python command to find where the ArcPy encode cp1252 is located and read it so that I can make a function that deals with it?</p> <pre><code># -*- coding: utf-8 -*- import sys print ('Loaded encoding : %(t)s'%{'t':sys.getdefaultencoding()}) reload(sys) # See stackoverflow question 2276200 sys.setdefaultencoding('utf-8') print ('Set default encoding : %(t)s'%{'t':sys.getdefaultencoding()}) print '' texte = u'Récuperation des données' print ('Original type : %(t)s'%{'t':type(texte)}) print ('Original text : %(t)s'%{'t':texte}) print '' import arcpy print ('imported arcpy') print ('Loaded encoding : %(t)s'%{'t':sys.getdefaultencoding()}) print '' print ('arcpy mess up original type : %(t)s'%{'t':type(texte)}) print ('arcpy mess up original text : %(t)s'%{'t':texte}) print '' print ('arcpy mess up reencoded with cp1252 type : %(t)s'%{'t':type(texte.encode('cp1252'))}) print ('arcpy mess up reencoded with cp1252 text : %(t)s'%{'t':texte.encode('cp1252')}) raw_input() </code></pre> <p>and when I run the script, I get these results : </p> <blockquote> <p>Loaded encoding : ascii<br> Set encoding : utf-8<br> <br> Original type : type 'unicode'<br> Original text : Récuperation des données&nbsp;&nbsp;&nbsp;<strong>&lt;--- This is right</strong><br> <br> import arcpy<br> Loaded encoding : utf-8<br> <br> arcpy mess up original type : type 'unicode'<br> arcpy mess up original text : R'cuperation des donn'es>&nbsp;&nbsp;&nbsp;<strong>&lt;--- This is wrong</strong><br> arcpy mess up ReEncode with cp1252 type : type 'str'<br> arcpy mess up ReEncode with cp1252 text : Récuperation des données>&nbsp;&nbsp;&nbsp;<strong>&lt;--- This is fits with the original unicode</strong><br></p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload