title: Python issue with incorrectly formated strings that contains \x
body: <p>At some point our python script receives string like that:</p>
  
  <pre><code>In [1]: ab = 'asd\xeffe\ctive'
  
  In [2]: print ab
  asd�fe\ctve \ \\ \\\k\\\
  </code></pre>
  
  <p>Data is damaged we need escape \x to be properly interpreted as \x but \c has not special meaning in string thus must be intact.</p>
  
  <p>So far the closest solution I found is do something like:</p>
  
  <pre><code>In [1]: ab = 'asd\xeffe\ctve \\ \\\\ \\\\\\k\\\\\\'
  
  In [2]: print ab.encode('string-escape').replace('\\\\', '\\').replace("\\'", "'")
  
  asd\xeffe\ctve \ \\ \\\k\\\
  </code></pre>
  
  <p>Output taken from IPython, I assumed that ab is a string not unicode string (in the later case we would have to do something like that:</p>
  
  <pre><code>def escape_string(s):
      if isinstance(s, str):
          s = s.encode('string-escape').replace('\\\\', '\\').replace("\\'", "'")
      elif isinstance(s, unicode):
          s = s.encode('unicode-escape').replace('\\\\', '\\').replace("\\'", "'")
      return s
  </code></pre>