Arcpy.mapping change text elemnts doesn't word with unicode

2391
3
04-25-2013 12:19 PM
LeticiaNascimento
New Contributor
Hello,

I am having a problem for days and I just can't figure it out. I have this code:

cenario = "1"
num = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[6]
num.text = "CENÁRIO " + cenario

cond = "Sizigia"    
textoGeral = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[9]
textoGeral.text =  "Condição" + cond


the first text ("CENÁRIO") is working just fine when it change the text element on my mxd. However, the situation below is not working. If I put only the string "Condição" the ç and the ã goes just fine, but when I add it with the variable "cond" it goes nuts and puts strange simbols instead (see picture below).

[ATTACH=CONFIG]23773[/ATTACH]


As you can see, the "CENÁRIO" WORKED just fine...

I looked some topics here on the forum and tryied everything.  In the begining of my code I have the

#!/usr/bin/python
# coding: utf-8


I also have tryed put an "u" before the text like:

textoGeral.text =  u"Condição" + cond


Can some tell me what is happening?

The full script I take the cond information from a txt file. The full code is:

#!/usr/bin/python
# coding: utf-8

import arcpy
import arcpy.mapping as MP
from datetime import datetime

startTime = datetime.now()

mxd = MP.MapDocument(r"p:\5270069_vli_tuf_dragagem_feL3\navigation\results\GIS_geral\Cenario_Basico_Santos_piacaguera_3.mxd")

DF1 = MP.ListDataFrames(mxd)[0]
Zoom1 = MP.ListDataFrames(mxd)[1]
Zoom2 = MP.ListDataFrames(mxd)[2]

projeto = raw_input("Digite o caminho com a pasta do projeto contendo os txts: ")


caminho_navios = projeto + "navios.txt"
f = open(caminho_navios,"r")
data = f.readlines()
numcenarios = len(data)


#Lendo o txt e separando as info
for i in range(0,numcenarios):
    vez = data.split('\t')
    cenario = vez[0]
    navio = vez[1]
    tipo = vez[2]
    rebocador = vez[3]
    condicao = vez[4]
    vento = vez [5]
    trackplot = vez[6]
    
    
#Changing layout elements...
    numero_projeto = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[4]
    numero_projeto.text = numProjeto

    num = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[6]
    num.text = "CENÁRIO " + cenario

    titulo_final = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[7]
    titulo_final.text = titulo
    
    textoGeral = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[9]
    textoGeral.text =  "Condição" + condicao

    # textoGeral = MP.ListLayoutElements(mxd, "TEXT_ELEMENT")[9]
    # textoGeral.text = "Condição: " + condicao

#Exportando pra PDF
    print "Exportando mapa do cenario " + cenario

    MP.ExportToPNG(mxd, projeto + "TUF_Cenario_" + cenario + ".png")
    print "PNG Exportado"

    MP.ExportToPDF(mxd, projeto + "TUF_Cenario_" + cenario + ".pdf")
    print "PDF Exportado"

#Salvando copia do MXD
    mxd.saveACopy(projeto + "TUF_Cenario" + cenario + ".mxd")
    print "Cenario " + cenario + " Finalizado"

print "Esta rotina demorou " + str(datetime.now()-startTime) + " horas"


Thank you
Tags (2)
0 Kudos
3 Replies
MikeHunter
Occasional Contributor
I tried your code, and it works just fine here.  I get "Condição Sizigia" in my layout text.  So I'm just guessing, but maybe it is your source code encoding. Make sure the text editor you are using saves the module file encoded to utf-8.  One other thing you might try is to change the coding header in your module from "# coding: utf-8" to "# -*- coding: utf-8 -*-"  I'm not sure either of these suggestions will help, but they are worth a try.   Like I said, I cannot repeat the issue you are having.

good luck,
Mike
0 Kudos
JeffBarrette
Esri Regular Contributor
I can't reproduce either but that doesn't mean there isn't an issue.  I need to know more about your system environment. Please answer the following:

Are you using an English OS ? If not, please specify.
Are you using an English ArcGIS Desktop or  a localized ArcGIS Desktop to reproduce the issue?

Please provides your workflow.

Thanks,
Jeff
0 Kudos
MikeHunter
Occasional Contributor
Let's look at this logically. The text element is storing the text attribute as a unicode string. We can confirm this in the Arcmap Python window:
>>> textoGeral
<TextElement object at 0x19a71b70[0x19a6d480]>

>>> print textoGeral.text
Condição: Sizigia

>>> textoGeral.text
u'Condi\xe7\xe3o: Sizigia'
>>> 


But you are feeding it a byte string. We can see that in your code. (utf-8 is not unicode, it is a bunch of bytes that represent unicode). So somewhere in the arcpy.mapping code, your bytes are getting coerced into a unicode string.  For some reason, that coercion is failing to work correctly on your machine (but not mine and Jeffery's). It seems to me that if we eliminate the need for the coercion, then you might get happier results.  I suggest you feed the text element pure unicode:

a = "Condição: ".decode('utf-8')
b = condicao.decode('utf-8')
txt = a + b
textoGeral.text = txt


There may be something deeper going on, so this may not help.  But it's worth a shot.

good luck,
Mike
0 Kudos