Dear Friends,
I'm having some troubles in my script connected with special characters.
I built a program that is supposed to process some images in a folder and give back some files (xls and txt).
Everything is fine in most of the cases, but if in the path (including filename) there is any non ascii character such as č I get the following error

'ascii' codec can't encode character u'\u010d' in position...:
ordinal not in range(128)

How can I handle this problem?

Thanks a lot for your help

For instance use

sys.setdefaultencoding('iso-8859-1')

I tried to put it at the beginning of my script but I get an AttributeError

'module' object has no attribute 'setdefaultencoding'

How about a magic comment at the begiining of your code?

# -*- coding: iso-8859-15 -*-

Nothing, again the same error

'ascii' codec can't encode character u'\u010d' in position...:

My script runs on python 2.6, may this be an issue? Would it be better to migrate to python 3 (if possible, I still didn't check if all the modules I use are supported in py3 as well)?
Thanks

u'\u010d'

This seems not to be in latin1 set of characters but is in utf8 (as it is way to encode all unicode characters)

>>> w = 'abc'+u'\u010d'
>>> with open('t.txt', 'w') as out:
    out.write(w.encode('utf8'))
    out.write('\n')


>>> print unicode(open('t.txt').read(), 'utf8')
abcč

Dear pyTony,
my problem is not with what I write into a txt file. The issue arise when I select a folder (or a file name with such a character) to be passed to another command. I basically create a string with some parameters for exiftool and I run it:

createtxt="exiftool.exe -q -m -fast2 -c \"%.6f\" -GPSLatitude# -GPSLongitude# -GPSAltitude# -GPSLatitudeRef# -GPSLongitudeRef# -GPSTimeStamp -GPSDateStamp -w! txt \""+inputpath+"\""
os.system(createtxt)

Thank you again,
G.

And encoding the unicode string does not help?

Same result with

subprocess.call(createtxt)

After I solved the unicode conversion (thanks guys for your help) it turned out to be also a problem with exiftool.
I google it for quite a while and the best solution I could find was to use the following

import win32api
win32api.GetShortPathName('path/to/file')

I still didn't find a way to make it to work but I should let you know.
Thanks

OK, it works!
in my first test I applied the GetShortPathName to the wrong variable.
Now it works perfectly.
Do you know if this may create problem on different (win) systems?
Thanks,
G.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.