I use the OptParse module to get a string value. OptParse only supports str typed strings , not unicode .
So let's say I start my script with:
./someScript
French characters, such as 'é', are typed by str , a UnicodeDecodeError trigger when reading in code:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 99: ordinal not in range(128)
I played a little with the built-in Unicode function, but either I get an error message or the character disappears:
>>> unicode('é'); Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) >>> unicode('é', errors='ignore'); u''
Is there anything you can do to use OptParse to extract unicode / utf-8 strings?
UPDATE
It seems that the line can be restored and printed fine, but then I try to use this line with sqlite (using the APSW module) and it tries to somehow convert to unicode with cursor.execute("...") , and then an error occurs.
Here is an example of a program that causes an error:
#!/usr/bin/python # coding: utf-8 import os, sys, optparse parser = optparse.OptionParser() parser.add_option("--some-option") (opts, args) = parser.parse_args() print unicode(opts.some_option)
user610650
source share