3 ms·
https://docs.python.org/3/library/codecs.html https://docs.python.org/3/library/codecs.html The encoding `unicode_escape` is not about escaping unicode charact
by maxnoe 7y ago
https://docs.python.org/3/library/codecs.html https://docs.python.org/3/library/codecs.html
The encoding `unicode_escape` is not about escaping unicode characters. It's about python source code. It's defined as:
> Encoding suitable as the contents of a Unicode literal in ASCII-encoded Python source code, except that quotes are not escaped.
It makes absolutely no sense to have escaped unicode characters as actual unicode string. If you really need that, a version that also works in python 2 would be:
u'hellö'.encode('unicode_escape').decode('ascii')
- dataflow 7y ago> It makes absolutely no sense to have escaped unicode characters as actual unicode string. Absolutely no sense? So is a basic Python expression evaluator like this complete nonsense to you? #!/usr/bin/env python3 try: # Python 2 from Tkinter import Tk, Entry, END import tkMessageBox as messagebox except ImportError: # Python 3 from tkinter import Tk, Entry, END, messagebox import ast def my_eval(s): # assume I've implemented this functionality manually... return ast.literal_eval(s) root = Tk() e = Entry(root) e.insert(END, '"Hell\\xc3\\xb6"') # Assume the user typed has typed in a Python expression, not me e.pack() root.bind('<Return>', lambda evt: messagebox.showinfo("Result", repr(my_eval(e.get())))) root.mainloop() I get an escaped string... because that's quite literally what Entry.get() gives me from the text box the user typed into. The simple fact that I got a string containing Python source code with escape characters makes "absolutely no sense" to you? Note that that wasn't even my choice! That was the choice of the built-in Python GUI toolkit... distributed by the same folks who decided this string/bytes overhaul was a brilliant idea...