Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing unicode \u2026 like characters in a string in python2.7 [duplicate]

I have a string in python2.7 like this,

 This is some \u03c0 text that has to be cleaned\u2026! it\u0027s annoying! 

How do i convert it to this,

This is some text that has to be cleaned! its annoying! 
like image 756
Sandeep Raju Prabhakar Avatar asked Mar 10 '13 10:03

Sandeep Raju Prabhakar


People also ask

How do I remove Unicode characters from a string in Python?

In python, to remove Unicode ” u “ character from string then, we can use the replace() method to remove the Unicode ” u ” from the string. After writing the above code (python remove Unicode ” u ” from a string), Ones you will print “ string_unicode ” then the output will appear as a “ Python is easy. ”.

How do I remove non ascii characters from a string in Python?

Remove Non-ASCII Characters From Text Python Here we can use the replace() method for removing the non-ASCII characters from the string. In Python the str. replace() is an inbuilt function and this method will help the user to replace old characters with a new or empty string.


1 Answers

Python 2.x

>>> s 'This is some \\u03c0 text that has to be cleaned\\u2026! it\\u0027s annoying!' >>> print(s.decode('unicode_escape').encode('ascii','ignore')) This is some  text that has to be cleaned! it's annoying! 

Python 3.x

>>> s = 'This is some \u03c0 text that has to be cleaned\u2026! it\u0027s annoying!' >>> s.encode('ascii', 'ignore') b"This is some  text that has to be cleaned! it's annoying!" 
like image 140
Burhan Khalid Avatar answered Oct 20 '22 12:10

Burhan Khalid