1

I work with lots of administrative data from Scandinavia and often included in the metadata are text strings with "ÅÄÄåäö"-characters. For example, a shapefile with city names.

What I would like to be able to do is search through the attribute table and replace each Å and Ä with A, Ö with o, å and ä with a and ö with o. I know how to do this with 'replace' but my problem is that even having the letters in the script causes an error.

I even replaced the actual letters with the appropriate chr-value but that didn't work either. Here's just one of many attempts to do this:

# -*- coding: cp1252 -*-    
from arcpy import da
import re

fc = r"V:\Indata\tatort_fran_kund_buff200m.shp"
fields = ['TOBETECKN', 'Outname']    

with da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        s = row[0]
        for ch in s:
            if ch == chr(196):
                s = s.replace(ch, 'A')
            elif ch == chr(197):
                s = s.replace(ch, 'A')
            elif ch == chr(214):
                s = s.replace(ch, 'O')
            elif ch == chr(228):
                s = s.replace(ch, 'a')
            elif ch == chr(229):
                s = s.replace(ch, 'a')
            elif ch == chr(246):
                s = s.replace(ch, 'o')
    row[1] = s
    cursor.updateRow(row)

Nothing I've tried works. In this example, the script worked but ignored the actual vowel replacement and returned the same text (with the old vowels). Any and all help is appreciated but please do not suggest that I read up on unicode or anything like that (I've been googling this for weeks w/o success).

Malachi
  • 21
  • 4
  • 1
    possible duplicate http://stackoverflow.com/questions/1342000/how-to-make-the-python-interpreter-correctly-handle-non-ascii-characters-in-stri – litepresence Apr 13 '17 at 14:07
  • Not quite... that question is looking to *remove* non-ASCII characters. – glibdud Apr 13 '17 at 14:11
  • What version of python are you using? Also, what is the encoding of the file you are opening (you can see it if you open it in a editor like notepad++ or sublime)? – wheeler Apr 13 '17 at 14:12
  • 1
    "please do not suggest that I read up on unicode or anything like that" well sorry but you WILL have to understand unicode and encodings to solve this problem. – bruno desthuilliers Apr 13 '17 at 14:14
  • Damn shapefile and the underlying DBase format....The encoding you're using match cpg file on shapefile ? – fn. Apr 13 '17 at 14:22

0 Answers0