4

Ok,

I have two strings, "Hello\nWorld!" and Hello\\nWorld!. I have to compare those that way that \n and \\n equals.

Thats not hard. I just string1.replace("\n", "\\n").

But what if I have to do it right for all escape chars including unicode escapes, so manual replace is not an option.

UPDATE

Exaple case:

I read from file Hello\nWorld! (as seen when file is opened in editor). Python will see Hello\\nWorld!

I want to compare the last one with first one the way they are equals.

Hannes Karppila
  • 969
  • 2
  • 13
  • 31
  • What is the bigger context here? Are you talking about the differences between printing a string and its [`repr()`](https://docs.python.org/2/reference/datamodel.html#object.__repr__)? Or differences between byte strings (`'foo'`) and unicode strings (`u'foo'`)? Or are these strings coming from an external source that already does some escaping? – Lukas Graf May 21 '14 at 15:50
  • 1
    `\x` is not a unicode escape, that's the way that unprintable *bytes* are displayed. You'll need to show some example input. – roippi May 21 '14 at 15:51
  • X was an example char (any char). Edited, hope it's now more clear. – Hannes Karppila May 21 '14 at 15:53

1 Answers1

10

How about using unicode_escape encoding?

>>> 'hello\r\n'.encode('unicode_escape') == 'hello\\r\\n'
True
>>> 'hello\r\n' == 'hello\\r\\n'.decode('unicode_escape')
True

In Python 3.x, you need to encode/decode the string/bytes:

>>> 'hello\r\n'.encode('unicode_escape').decode() == 'hello\\r\\n'
True
>>> 'hello\r\n' == 'hello\\r\\n'.encode().decode('unicode_escape')
True
falsetru
  • 357,413
  • 63
  • 732
  • 636