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'))

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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.
i had to encode the first and last strings in order to get rid of the errot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.