Select to view content in your preferred language

æøå and python

2975
5
07-09-2010 12:59 AM
MortenTranekjaer
New Contributor
Hi,

I'm struggling with a small python script. What I want is that the script should export all selected attributes from at select by location operation (on two feature layers) to a text file. The script works fine when attribute data doesn't contain æøå, but when æøå is in it only returns (write to txt file)those attributes (road names) from the top in which æøå is not a part of det text string. To me it seems as if the cursor stop when it bumps into æøå. I'm sure it's a matter of encoding, but i don't know how to solve this? 😞

Does anyone know how to solve this issue? Here is the code (it is the field "VEJNAVN" that contains æøå):

******
import sys, arcgisscripting, string
gp = arcgisscripting.create(9.3)

ADRESSER = sys.argv[1]
gp.overwriteoutput = 1
try:
    logfile = "C:\\Documents and Settings\cg21mj\\Skrivebord\\logfile.txt"
    log = open(logfile, "w")
    log.write  ("OBJECTID"+";"+"KOMMUNENR"+";"+"VEJNAVN"+";"+"VEJKODE"+";"+"HUSNUMMER"+";"+"NKLASSE"+";"+"TSTAND\n")
    rows = gp.SearchCursor(ADRESSER)
    row = rows.Next()
    while row <> "":
        result0 = row.GetValue("OBJECTID")
        result1 = row.GetValue("KOMMUNENR")
        result2 = row.GetValue("VEJNAVN")
        result3 = row.GetValue("VEJKODE")
        result4 = row.GetValue("HUSNUMMER")
        result5 = row.GetValue("NKLASSE")
        result6 = row.GetValue("TSTAND")
        log.write(str(result0) + ";" + str(result1) + ";" + str(result2) + ";" + str(result3) + ";" + st(result 4) + ";" + str(result5) + ";" + str(result6) + "\n")
        row = rows.Next()
    log.close()
except:
    print gp.GetMessages()

*****

Thanks in advance!

Regards,
Morten
GIS coordinator, Denmark
0 Kudos
5 Replies
GünterDörffel
Occasional Contributor III
Hi Morten,

python is sensible in special characters ... we have them a lot in germany, too 🙂

Try adding a starting line to your python script telling it to work in unicode:

 
# -*- coding: utf-8 -*-


This LOOKS like a comment AND NEEDS TO BE THE FIRST LINE OF THE SCRIPT ... but it is important!
You might want to use a different codepage ... or even go further and use the python concepts of
.encode() and .decode() ... but usually declaring the codepage to work in will do!

Regards
Guenter
0 Kudos
MortenTranekjaer
New Contributor
Hi Guenter,

Thanks for your very quick reply 🙂

I already tried the utf-8 codepage yesterday, but it doesn't work, hmm! I will give it a try with the .encode() and .decode concept.

Best regards,
Morten
0 Kudos
MortenTranekjaer
New Contributor
Hi Guenter,

I found a quick and easy solution!

I just added the following lines to the scritpt and VOILA it works 😄

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

Regards,
Morten
0 Kudos
GünterDörffel
Occasional Contributor III
Hi Morten,

I'll add that to my troubleshoot-list ... thanks and glad it works now!

Regards
Guenter
0 Kudos
Luke_Pinner
MVP Regular Contributor
If you ever want to distribute your code, you're better off explicitly using encode/decode.

"sys.setdefaultencoding is evil"
http://aspn.activestate.com/ASPN/Mail/Message/python-dev/3744296
0 Kudos