Skip to content Skip to sidebar Skip to footer

Python 3.5 Not Handling Unicode Input From Cli Argument

I have a simple script that I'm attempting to use automate some of the japanese translation I do for my job. import requests import sys import json base_url = 'https://www.goog

Solution 1:

changing

print(sys.argv[1])

to

print(sys.argv[1].encode("utf-8"))

Will cause python to dump a string of bytes

$ python google_translate.py 初期設定クリア
 b'\xe5\x88\x9d\xe6\x9c\x9f\xe8\xa8\xad\xe5\xae\x9a\xe3\x82\xaf\xe3\x83
 \xaa\xe3\x82\xa2'

Nonetheless it works. So the bug, if this is a bug... Is happening when python is decoding the internal string to print into the terminal, not when the argument is being encoded INTO a python string.

Also simply removing the print statement fixes the bug as well.

Post a Comment for "Python 3.5 Not Handling Unicode Input From Cli Argument"