arcpy featureset encoding problem

4252
2
01-21-2015 07:45 AM
MattiasEkström
Regular Contributor

I've written a pythonscript that queries a mapservice layer then populates a featureset with the result and then export the attributes to an Excelfile with arcpy.TableToExcel_conversion. The problem is that once the data is loaded into the featureset any special characters like å ä ö gets messed up. How can I make sure that all characters are correct in the FeatureSet and the result Excelfile?

Here is some sample code that shows the problem:

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


import arcpy, urllib2, os


#baseURL = arcpy.GetParametersAsText(0)
baseURL = "http://services.arcgis.com/hrh7HJqpzvUrwY6X/arcgis/rest/services/Sveriges_l%C3%A4n/FeatureServer/0"
#where = arcpy.GetParametersAsText(1)
where = "1=1"
where = urllib2.quote(where)
query = "/query?where={}&outFields=*&returnGeometry=false&f=json".format(where)
queryURL = baseURL + query


print queryURL
JSONrespons = urllib2.urlopen(queryURL).read()
print "JSONrespons where å ä ö displays correct: "
print JSONrespons
print "\n-----------------------------------------------------------------\n"


fs = arcpy.FeatureSet()
filename = "Table.xls"


try:
    fs.load(queryURL)
    print "JSON extracted from recordset where å ä ö displys incorrect:"
    print fs.JSON
    try:
        arcpy.TableToExcel_conversion(fs,filename)
        #Excel-file from feature_set also displays å ä ö incorrect...
        outputFile = os.path.join(arcpy.env.scratchFolder, filename)  
        arcpy.SetParameterAsText(2, outputFile)
    except Exception, e:
        print e.message
    
except Exception, e:
    print e.message
0 Kudos
2 Replies
StabsstelleGIS
New Contributor III

Hi,

I've been encountered similar problems. Have you tried this at the beginning of your code:

reload(sys)

sys.setdefaultencoding('UTF8')

Maybe this blogpost will help you too

Andre

0 Kudos
MattiasEkström
Regular Contributor

Thanks for your reply, unfortunately that didn't work. And the blogpost you mentioned is above my python skills, it didn't help me solve the problem either.

0 Kudos