Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing \u2018 and \u2019 character

Tags:

python

I am using Beautiful Soup to parse webpages and printing the name of the webpages visited on the terminal. However, often the name of the webpage has single left (\u2018) and right(\u2019) character which the python can't print as it gives charmap encoding error. Is there any way to remove these characters?

like image 867
bhavesh Avatar asked Jun 23 '14 04:06

bhavesh


People also ask

What character is u2019?

The Unicode character ' (U+2019 RIGHT SINGLE QUOTATION MARK) is used both for a typographic apostrophe and a single right (closing) quotation mark.

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.


1 Answers

These codes are Unicode for the single left and right quote characters. You can replace them with their ASCII equivalent which Python shouldn't have any problem printing on your system:

>>> print u"\u2018Hi\u2019" ‘Hi’ >>> print u"\u2018Hi\u2019".replace(u"\u2018", "'").replace(u"\u2019", "'") 'Hi' 

Alternatively with regex:

import re s = u"\u2018Hi\u2019" >>> print re.sub(u"(\u2018|\u2019)", "'", s) 'Hi' 

However Python shouldn't have any problem printing the Unicode version of these as well. It's possible that you are using str() somewhere which will try to convert your unicode to ascii and throw your exception.

like image 120
14 revs, 12 users 16% Avatar answered Sep 21 '22 22:09

14 revs, 12 users 16%