I am trying to compare some files to a datalist. The datalist's names are all in english letters, while the datafiles have the scandinavian "æ" "ø" "å" letters.

an example: the file "Kystnære_måger.shp" and the datalist name "kystnaere_maager" are compared using

filename_UK = ('Kystnære_måger'.lower().replace("ø", "oe")
.replace("æ", "ae").replace("å", "aa").replace(".shp", ""))
dataname = "kystnaere_maager"

print (dataname == filename_UK)

In Pyscripter, using a default Python 3, the value returned is True. If the above statement is run in the python EDITOR window (not the python console) in Qgis 3.2, the returned value is False

can anyone give any explanation for this, or some way to circumvent it? I need to use the editor, as the python console in qgis doesnt handle many lines of code very well.

to be precise, the actual output from the qgis python editor is: exec(open('C:/Users/JonJon/AppData/Local/Temp/tmpnzhq2w9i.py'.encode('utf-8')).read()) False

  • This looks like an encoding issue. How do you use this code? As a processing script? Are the names by chance loaded using the resulting GUI (as in: select .shp or other files)? If so, that might be the root of issues: QGIS dialogs by default open files in system encoding (which might not be UTF-8, which is usually used when coding nowadays). There's a number of Qs about this, e.g. gis.stackexchange.com/questions/279765/…
    – Senshi
    Commented Jul 10, 2018 at 13:14

1 Answer 1


You need to move lower() to the end after you do replace():

filename_UK = ('Kystnære_måger'.replace("ø", "oe")
.replace("æ", "ae").replace("å", "aa").replace(".shp", "").lower())
dataname = "kystnaere_maager"

print (dataname == filename_UK)

>>> exec(open('C:/Users/Joseph/AppData/Local/Temp/tmprnczrpgr.py'.encode('utf-8')).read())
  • 1
    It worked! but what about situations where the file starts with a scandinavian letter, i.e. "Øer" (means Islands in danish)? and what caused the errror, do you know?
    – Jonas
    Commented Jul 10, 2018 at 13:32
  • @NielsJonasEnokOlsen - QGIS had problems when reading and saving files with non-ascii characters. Not sure if this has been resolved in QGIS 3. In terms of the error, I'm not exactly sure. My guess is that the lower() method changes the encoding of the string so that the non-ascii characters become other characters which are not mentioned in your replace() methods (this is just a guess!).
    – Joseph
    Commented Jul 10, 2018 at 13:45

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.