2

I'm trying to export with python 2.7 some shapefiles and CAD files that could have specials characters in their filename, like accent marks. In example, I want to have this:

MMT_LT_1215_20101004_05_Crespià_Esponellà.shp

But when I export the files the filename is like this:

MMT_LT_1215_20101004_05_CrespiÓ_EsponellÓ.shp

I've already changed the encoding to UTF-8 with # -*- coding: utf-8 -*-, but it don't result anyway. The fact is that when I create the directory that must contain the files and has the same file name, the accent marks are writed correctly like in the first example. I'm not sure if I have to use the encode() or decode() functions to face that.

  • 2
    I think you should avoid special charachters in file names. Would normalizing the charachters be an option? unicodedata.normalize('NFD','á å ä Ö').encode('ascii','ignore').decode('utf-8') output: 'a a a O' – BERA Apr 3 at 9:03
  • I guess it would be an option, but I tried this unicodedata.normalize('NFD', nomMuni1).encode('ascii', 'ignore').decode('utf-8') and the result is the same. nomMuni1 is the variable that contains the text to normalize. – Fran Martin Rivas Apr 3 at 9:48
  • 1
    It works, thanks so much! – Fran Martin Rivas Apr 3 at 10:29
  • The Shapefile specification actually states that names should be 8.3 compliant, so using UTF-8 names actually presents a portability risk. – Vince Apr 3 at 11:00
3

I think you should avoid special characters in file names. You can normalize them using unicode.normalize, for example:

from unicodedata import normalize
somestring = 'á å ä Ö'
somestring = normalize('NFD',somestring).encode('ascii','ignore').decode('utf-8') 

'á å ä Ö' will now be 'a a a O'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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