Dear all,
I have a small script wich is supposed to process two images passed as arguments in commandline, but I don't know how to handle these arguments when they are with non latin characters.
Here is an example of command

python.exe myscrit.py --first=xvalue "image1.png" "οισξφσιο.png"

If I isolate the arguments as follows

import sys, getopt
opts, args = getopt.getopt(sys.argv[1:], '', ['first='])    opts = dict(opts)
fval_name = opts.get('--first', 'xvalue')

and I print the second args I end up with a string like this ??s?fs??.png.
What am I doing wrong and how can I fix it?

I also tried the following in my script but nothing seems to work

# -*- coding: UTF-8 -*-
from __future__ import unicode_literals

import sys, traceback
reload(sys)
sys.setdefaultencoding("utf-8")

Any help is appreciated.
Thanks,
giancan

Recommended Answers

All 3 Replies

This is a windows issue, so I cannot help you much, furthermore, it seems to be a python 2 issue only. You could try solutions such as <this one> for example.

The best thing to do is probably to move to python 3. Try with your install of python 3 if the same problem occurs. Also the getopt module is a tedious way to handle command line arguments, choose argparse instead (my favorite module for this is argh, see it in pypi).

You need to learn how to deal with Unicode characters in Python.

Suggested reading: https://docs.python.org/2/howto/unicode.html

No need to use Python3. Some people ignore the obvious solutions which are built in to Python2 in order to push their political agenda (getting more people to use Python3).

@Dave_15 I don't quite agree with you. First your post contains no obvious solution for this specific issue. Second I don't have a political agenda telling me to get more people to use python 3. Concerning unicode characters, many python 2 issues simply vanish by using python 3 because unicode is correctly handled in the language's very design. So why waste time solving issues that exist only because one uses an obsolete version of python ?

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.