Hướng dẫn python decode escaped string
I have some escaped strings that need to be unescaped. I'd like to do this in Python. For example, in Python 2.7 I can do this:
How do I do it in Python 3? This doesn't work:
My goal is to be able to take a string like this:
And turn it into:
After I do the conversion, I'll probe to see if the string I have is encoded in UTF-8 or UTF-16.
SuperStormer 4,7515 gold badges20 silver badges32 bronze badges asked Feb 11, 2013 at 20:37
3 You'll have to use
If you start with a If
you need bytes as end result, you'll have to encode again to a suitable encoding ( Your example is actually UTF-16 data with escapes. Decode from
answered Feb 11, 2013 at 20:40
Martijn Pieters♦Martijn Pieters 984k273 gold badges3872 silver badges3234 bronze badges 4 The old "string-escape" codec maps bytestrings to bytestrings, and there's been a lot of debate about what to do with such codecs, so it isn't currently available through the standard encode/decode interfaces. BUT, the code is still there in the C-API (as
These functions return the transformed
answered Apr 18, 2014 at 9:57
1 If you want str-to-str decoding of escape sequences, so both input and output are Unicode:
Testing:
answered Nov 13, 2019 at 2:33
MestreLionMestreLion 11.9k4 gold badges62 silver badges55 bronze badges 3 You can't use This function implements
answered Mar 20, 2013 at 15:59
malthemalthe 97510 silver badges21 bronze badges py2
py3
answered Feb 22, 2021 at 15:22
SérgioSérgio 6,6981 gold badge46 silver badges51 bronze badges 2 At least in my case this was equivalent:
convertutils.py:
answered Aug 13, 2019 at 15:50
guettliguettli 23.8k66 gold badges307 silver badges581 bronze badges |