2

I need to store into a dictionary data from a shapefile that contains in several fields unrecognizable characters by ASCII, like in this one: "Städtebaulicher Entwicklungsbereich: Sanierungsgebiete SG 1 u. SG 2, Mkt. Floß". In the end, I have to create a report in Excel with all these attributes.

I get the data ( stringVal ) by using SearchCursor. This is a simplified code of how I store the data in the dictionary:

cursor=arcpy.da.SearchCursor(filename_path,["Category"])
for row in cursor:            
        stringVal=row[i]
        if stringVal not in d:
            d[stringVal]=1
        else:
            d[stringVal]=d[stringVal]+1    

So my dictionary should look like this:

d={"Städtebaulicher Entwicklungsbereich: Sanierungsgebiete SG 1 u. SG 2, Mkt. Floß":1,"Städtebaulicher Entwicklungsbereich: Sanierunggebiet Südstadt, St. Grafenwöhr":4}

At this moment, it looks like this, when printing it:

d={u'Bayerisches Staatsministerium der Finanzen, f\xfcr Landesentwicklung und Heimat': 11}

I know it has to do with unicodes and UTF-8, but from what I read on the internet, I still couldn't solve it.

4
  • What is your code snippet to "get the data by using SearchCursor"?
    – PolyGeo
    Nov 17, 2017 at 19:58
  • I updated now the original post.
    – panda
    Nov 17, 2017 at 20:27
  • I think the problem is not about unicode. The print shows another text that is different from your text.I think the searchcursor reads another record or maybe another field.
    – BBG_GIS
    Nov 19, 2017 at 22:23
  • The SearchCursor reads the proper value from the proper field.
    – panda
    Nov 19, 2017 at 22:47

2 Answers 2

1

I found the solution for my problem.

In the first line of the script I kept using this:

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

while the correct form is:

# -*- coding: UTF-8 -*-

In the dictionary, the names still look weird but this doesn't matter, because now the script is able to read the data from the dictionary properly.

1
  • That's Great.your code works without it. Just handle unicodes in the code.It can be a challenge for you to learn using unicodes in python if you'll use unicode often
    – BBG_GIS
    Nov 19, 2017 at 23:00
1

I suppose your full code is like the below code.I don't know where you are using "i" in row[i]. But i set it to 0. It's mean i use "Category" field.

Note: when you want to print a dictionary with unicode strings, use rep and decode.The result of your print shows another words that is not about unicode

import arcpy
arcpy.env.workspace = r"..\folder"
d={}
cursor=arcpy.da.SearchCursor(filename,["Category"])
for row in cursor:
        stringVal=row[0]
        if stringVal not in d:
            d[stringVal]=1
        else:
            d[stringVal]=d[stringVal]+1

print repr(d).decode("unicode-escape")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.