1

here is the result I print out by python:

With \u003cb\u003eall\u003c/b\u003e respect, if we look from one perspective, it is just like looking at ants.

and the data type is

<type 'unicode'>

Is there gonna be a way to replace \u003cb\u003e by ''? I have tried

str.replace("\u003cb\u003e", ''), str.replace("\\u003cb\\u003e", '') and str.replace("<b>", '') but none of them worked

. How can properly replace it by an empty string?

edited:

here is the result of print repr(mystrung):

With \\u003cb\\u003eall\\u003c/b\\u003e respect, if we look from one
perspective, it is just like looking at ants.
2
  • 1
    It's unclear what's part of the string itself and what's just part of its representation. What does print(repr(your_string)) show? Commented Oct 3, 2017 at 21:16
  • It looks like your unicode string has been badly constructed. This: u'With \u003cb\u003eall\u003c/b\u003e respect' is just the same as u'With <b>all</b> respect' because u'\u003c' is u'\x3c' and actually is '<'. You should try to fix it upstream... Commented Oct 3, 2017 at 21:56

1 Answer 1

2

If you actually want to remove them completely, your second example should have worked. Using Unicode strings is more efficient, though, since an implicit conversion is eliminated:

>>> s=u'With \\u003cb\\u003eall\\u003c/b\\u003e respect, if we look from one perspective, it is just like looking at ants.'
>>> s.replace(u'\\u003cb\\u003e',u'').replace(u'\\u003c/b\\u003e',u'')
u'With all respect, if we look from one perspective, it is just like looking at ants.'

If you'd rather just convert the Unicode escapes, encoding a Unicode string containing only ASCII codepoints with ascii converts it back to a byte string, then decode it with unicode-escape to turn the literal escape codes back to characters:

>>> print(s.encode('ascii').decode('unicode-escape'))
With <b>all</b> respect, if we look from one perspective, it is just like looking at ants.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.