36

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 Python can't print as it gives a charmap encoding error.

Is there any way to remove these characters?

cottontail
  • 10,268
  • 18
  • 50
  • 51
bhavesh
  • 1,343
  • 2
  • 14
  • 23

2 Answers2

63

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.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
0

Since single characters (\u2018 and \u2019) need to be replaced, str.translate() could be used as well.

s = "\u2018Hi\u2019"
s_new = s.translate(str.maketrans({'\u2018': "'", '\u2019': "'"}))

# or even use the unicode characters themselves
s_new = s.translate(str.maketrans({'‘': "'", '’': "'"}))
print(s_new)   # 'Hi'

Or specify the encoding in the first place when parsing.

# if parsing an html
soup = BeautifulSoup(html, from_encoding='utf-8')

# if reading a file
with open("text.txt", encoding='utf-8') as f:
     f.read()
cottontail
  • 10,268
  • 18
  • 50
  • 51