1

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3

I am facing this issue while running a mailing script in py 2.7 for the line...

msg.attach(MIMEText(welcome_msg + htmlMessageContent + footer_msg, 'html'))

BoarGules
  • 16,440
  • 2
  • 27
  • 44
kb hithesh
  • 93
  • 8
  • Don't assume that the string is utf-8 encoded. See here: https://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte – lhay86 Feb 22 '18 at 12:36
  • Possible duplicate of [Python - Finding unicode/ascii problems](https://stackoverflow.com/questions/2753022/python-finding-unicode-ascii-problems) – tripleee Feb 22 '18 at 14:22

1 Answers1

1

One of the elements of the string you are concatenating

welcome_msg + htmlMessageContent + footer_msg

is Unicode, and another of them isn't. When you concatenate the strings Python has convert them all to a common type (Unicode), much as it does when you add an integer to a float. But the default string conversion to Unicode is ascii, and if the string contains a non-ascii character it will fail.

Find out which string isn't Unicode. For this you can use type(). Wrap that string in a call to unicode() that explains how you want '\xe3' interpreted. For example, if '\xe3' should be interpreted as 'ã':

unicode(mystring, encoding='Latin-1')

Then your concatenation should work.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • This obviously assumes the other stringtis Latin-1 which could be completely wrong. The real solution is to know what you have, and take it from there. – tripleee Feb 22 '18 at 14:23
  • That was a guess, and a reasonable one. If OP doesn't want want '\xe3' interpreted as 'ã' then he will need to find the encoding he needs. But to provide a testable solution I had to put in a real encoding name. Answering a question at this level with `encoding='your encoding goes here'` is, in my opinion, not all that helpful. Your comment assumes that OP knows what an encoding is. – BoarGules Feb 22 '18 at 14:25
  • 1
    The problem is more that the OP posted a fundamentally incomplete question, but IMHO, it bears pointing out that *blindly* applying a workaround without understanding it properly will just introduce new bugs. – tripleee Feb 22 '18 at 14:32
  • i had to encode the first and last strings in order to get rid of the errot – kb hithesh Feb 23 '18 at 05:35