<p>I wrote a piece of code to convert PHP's striplashes into valid Python [backslash] escapes:</p>

<pre><code>cleaned = stringwithslashes
cleaned = cleaned.replace('\\n', '\n')
cleaned = cleaned.replace('\\r', '\n')
cleaned = cleaned.replace('\\', '')
</code></pre>

<p>How can I condense it? </p>

[{'answer_id': 13456, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 0, 'selected': False, 'text': '<p>You can obviously concatenate everything together:</p>\n\n<pre><code>cleaned = stringwithslashes.replace("\\\\n","\\n").replace("\\\\r","\\n").replace("\\\\","")\n</code></pre>\n\n<p>Is that what you were after? Or were you hoping for something more terse?</p>\n'}, {'answer_id': 13579, 'author': 'eplawless', 'author_id': 1370, 'author_profile': 'https://Stackoverflow.com/users/1370', 'pm_score': -1, 'selected': False, 'text': '<p>Python has a built-in escape() function analogous to PHP\'s addslashes, but no unescape() function (stripslashes), which in my mind is kind of ridiculous.</p>\n\n<p>Regular expressions to the rescue (code not tested):</p>\n\n<pre><code>p = re.compile( \'\\\\(\\\\\\S)\')\np.sub(\'\\1\',escapedstring)\n</code></pre>\n\n<p>In theory that takes anything of the form \\\\(not whitespace) and returns \\(same char)</p>\n\n<p>edit: Upon further inspection, Python regular expressions are broken as all hell;</p>\n\n<pre><code>&gt;&gt;&gt; escapedstring\n\'This is a \\\\n\\\\n\\\\n test\'\n&gt;&gt;&gt; p = re.compile( r\'\\\\(\\S)\' )\n&gt;&gt;&gt; p.sub(r"\\1",escapedstring)\n\'This is a nnn test\'\n&gt;&gt;&gt; p.sub(r"\\\\1",escapedstring)\n\'This is a \\\\1\\\\1\\\\1 test\'\n&gt;&gt;&gt; p.sub(r"\\\\\\1",escapedstring)\n\'This is a \\\\n\\\\n\\\\n test\'\n&gt;&gt;&gt; p.sub(r"\\(\\1)",escapedstring)\n\'This is a \\\\(n)\\\\(n)\\\\(n) test\'\n</code></pre>\n\n<p>In conclusion, what the hell, Python.</p>\n'}, {'answer_id': 13598, 'author': 'dbr', 'author_id': 745, 'author_profile': 'https://Stackoverflow.com/users/745', 'pm_score': 4, 'selected': False, 'text': "<p>Not totally sure this is what you want, but..</p>\n\n<pre><code>cleaned = stringwithslashes.decode('string_escape')\n</code></pre>\n"}, {'answer_id': 13608, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 2, 'selected': False, 'text': '<p>It sounds like what you want could be reasonably efficiently handled through regular expressions:</p>\n\n<pre><code>import re\ndef stripslashes(s):\n    r = re.sub(r"\\\\(n|r)", "\\n", s)\n    r = re.sub(r"\\\\", "", r)\n    return r\ncleaned = stripslashes(stringwithslashes)\n</code></pre>\n'}, {'answer_id': 21862975, 'author': 'Jorgesys', 'author_id': 250260, 'author_profile': 'https://Stackoverflow.com/users/250260', 'pm_score': 1, 'selected': False, 'text': '<p>use <code>decode(\'string_escape\')</code></p>\n\n<pre><code>cleaned = stringwithslashes.decode(\'string_escape\')\n</code></pre>\n\n<p>Using </p>\n\n<blockquote>\n  <p><strong>string_escape</strong> : Produce a string that is suitable as string literal in Python source code</p>\n</blockquote>\n\n<p>or concatenate the replace() like Wilson´s answer.</p>\n\n<pre><code>cleaned = stringwithslashes.replace("\\\\","").replace("\\\\n","\\n").replace("\\\\r","\\n")\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/13454', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']