Search code examples
pythonbatch-filedecodeencode

python: batch string en- / decoding


i have following problem:

i wrote a python script and it needs inputparameters to run... but if the parameters include one of our german "umlaute" like äüö or ß the script stops with following error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 8: ordinal not in range(128)

and if i start the script with a batchfile, the "umlaute" are replaced with random chars like ?, some other variation of the ö....

pls help me.. thx :)

part of the code:

...
if batch_exe:
    try:
        aIndex = sys.argv.index("-a")
                buchungsart_regEx = sys.argv[aIndex+1]
        except:
                buchungsart_regEx = ""
else:
    ...
select_stmt = select_stmt + " AND REGEXP_LIKE (BUCHUNGSART, " + "'" + buchungsart_regEx + "')" 
...
db_list = sde_conn.execute(select_stmt) 
...

and the cmdinput is something like:

python C:\...\Script.py -i ..... -a äöüß

Solution

  • Check this answer: https://stackoverflow.com/a/846931/1686094

    You can use his sys.argv = win32_unicode_argv()

    And maybe you can then encode your sys.argv with utf-8 for future use.