I have a long string, which includes the text Your Sunday evening order with Uber Eats\nTo: [email protected]\n\n\n[image: map]\n\n[image: Uber logo]\n\xe2\x82\xac17.50\nThanks for choosing Uber,
I am trying to replace '\xe2\x82\xac' with 'EUR' in Python 3.6
If I print the string, I see that it is preceded by b, i.e. it is a byte literal.
b'<div dir="ltr"><br ...' etc. I cannot encode it (html = html.encode('UTF-8')), because then I get a bytes-like object is required, not 'str' nor can I decode it ('str' object has no attribute 'decode')
I have tried:
html = html.replace(u"\xe2\x82\xac","EUR") html = html.replace(u'\xe2\x82\xac',"EUR") html = html.replace('\xe2\x82\xac',"EUR") html = html.replace(u"€","EUR") None of these work.
html.decode("utf-8") gets me an error 'str' object has no attribute 'decode'.
For context, the string is generated by reading the content of an e-mail with the mailbox library:
for message in mbox: for part in message.walk(): html = str(part.get_payload(decode=True))
replaceworked for me, in python 3.6html.replace('\xe2\x82\xac',"EUR")just works in utf-8 text.# -*- coding: utf-8 -*-on the topu"string"prefix is unnecessary in python3