Reputation: 91630
I have a fairly simple program which grabs values from a file on the filesystem and allows the user to format them and print them. An average invocation of the script looks like this:
get-amazing-temperature -c 0 --format "%.2f"
I use that format
argument to format the value so that users of the script can format numbers in clever, creative ways. The problem comes, of course, when they want to have a custom format with the degrees (U+00B0) symbol.
I'm using argparse
underneath the hood. When I try something like this:
get-amazing-temperature -c 0 --format '%.2f\u00B0'
...the string literal is printed:
36\u00B0
How can I allow in unicode literals in command line arguments? I'll only be running this script on Linux/Unix machines.
Upvotes: 0
Views: 173
Reputation: 251373
When you process the argument containing the format string, you could decode it with the "unicode-escape" codec. This will convert unicode escapes to their corresponding characters:
>>> print x
%.2f\u00B0
>>> print x.decode('unicode-escape')
%.2f°
Upvotes: 2