Skip to main content
23 events
when toggle format what by license comment
Nov 14, 2024 at 3:45 answer added zozo timeline score: 1
Aug 8, 2024 at 19:51 comment added wim The best approach, which nobody seems to have even mentioned, is to find out why and how the data was escaped in the first place - you can't know the correct "unescaping" algorithm otherwise. Same story for decoding bytes and "guessing" the encoding, it's just impossible to do in the general case.
Jun 3, 2023 at 19:59 comment added NaN Maybe you can try using eval? For example, print(eval('"spam\\neggs"')) prints your desired output, and of course you may need to add/adjust some quotes before/after your original string.
Aug 7, 2022 at 9:40 comment added Karl Knechtel For the opposite problem - converting from "special" characters into escape sequences - see Python print string like a raw string. However, note that this is not a round-trip conversion; there are multiple ways to represent a given string with escape sequences, and only one of them is particularly easy to get.
Aug 6, 2022 at 1:00 comment added Karl Knechtel Note that most of these approaches will work with bytes input - for the ones that involve converting to bytes first, just skip that step. Similarly, str output can be converted to bytes if needed by simply using an appropriate encoding - latin-1 is probably what you want.
Aug 5, 2022 at 2:40 comment added Karl Knechtel Related: stackoverflow.com/questions/43662474/…
Aug 5, 2022 at 1:52 comment added Karl Knechtel Related: stackoverflow.com/questions/63218987/…
Aug 5, 2022 at 1:14 answer added Karl Knechtel timeline score: 2
Feb 23, 2022 at 0:36 comment added SuperStormer Related: how do I .decode('string-escape') in Python3?
Feb 12, 2021 at 4:25 review Close votes
Feb 12, 2021 at 11:51
Dec 17, 2020 at 1:41 comment added Glen Whitney I disagree with Apalala; using unicode_escape (on a properly latin1-encoded input) is completely reliable, and as the issue that Hack5 links to in his comment to user19087's answer shows, is the method recommended by the python developers.
Dec 17, 2020 at 1:26 answer added Glen Whitney timeline score: 5
Mar 4, 2019 at 22:45 answer added LimeTr33 timeline score: -1
Feb 19, 2019 at 19:22 history edited ShadowRanger CC BY-SA 4.0
Update fragment identifier to match current docs
May 5, 2016 at 20:27 answer added user19087 timeline score: 51
Jul 1, 2014 at 22:59 comment added Apalala Most answers to this question have serious problems. There seems to be no standard way to honor escape sequences in Python without breaking unicode. The answer posted by @rspeer is the one that I adopted for Grako as it so far handles all known cases.
Jul 1, 2014 at 21:12 answer added OpenAI stole this from rspeer timeline score: 183
Oct 26, 2010 at 6:11 comment added dln385 @Nas Banov That's a good test. That string contains no escape sequences, so it should be exactly the same after processing. myString = "'spam'+\"eggs\"+'''some'''+\"\"\"more\"\"\"", print(bytes(myString, "utf-8").decode("unicode_escape")) seems to work.
Oct 26, 2010 at 6:06 vote accept dln385
Oct 26, 2010 at 5:05 comment added Nas Banov hmmm, how exactly would you expect a string containing 'spam'+"eggs"+'''some'''+"""more""" to be processed?
Oct 26, 2010 at 5:01 answer added Jerub timeline score: 192
Oct 26, 2010 at 3:50 answer added Greg Hewgill timeline score: 12
Oct 26, 2010 at 3:43 history asked dln385 CC BY-SA 2.5