pythonpython-2.7arcmap

How can I replace 'ß' by 'ss' in an attribute table of a shapefile (arcmap)? ASCII error


I'm really new at python coding so please answer in detail and not too harsh.. I'm trying to replace the German umlaut 'ß' in an attribute table of a shapefile by 'ss' and am doing this by using the field calculator where you can add a python code block.

This is what I've tried so far:

def ecode(file, name, test):
    test.decode("utf-8")
    test.replace("\xe1", "ss")
    test.encode("utf-8")
    return test

Instead of "\xe1" I've also used "U+00DF" and "\xdf".

This error message occurs:

ERROR: ascii codec can't encode character u'\xdf' in position 11: ordinal not in range(128)

The streetname in this field of the attribute table is 'Zuccalistraße 21a', so obviously ß is the problem which is out of the ASCII range (there it >number 200). What can I do to replace it? I've searched the internet for 5 hours now....

Would love to get some answers! Kind regards, Ayla


Solution

  • So obviously, it's a problem with the decoding. When I try

    def ecode(file, name, test):
        test=test.decode("utf-8")
        test=test.replace(u"\xdf", "ss")
        test=test.encode("utf-8")
        return test
    

    I get the Error message:

    File "C:\Python27\ArcGIS10.2\Lib\encodings\utf_8.py", line16, in decode return codecs.utf_8_decode(input, errors, True)

    UnicodeEncodeError: 'ascii' codec can't encode character u'\xdf' in position 11: ordinal not in range(128)

    Now I got an answer to the problem: I found that when you enter

    import sys
    reload(sys)
    sys.setdefaultencoding("utf8")
    

    to the function, it works fine!! So thanks for trying to help me, have a nice day :)

    Cheers, Ayla