I'm trying to import email into a web app and when opening the email in the web app it shows the text like so:

"b'Your ticket has new correspondence added by x. \r\n\r\nView this ticket on the Client Portal at clientportal.com\r\n\r\nAlternatively you can view ticket updates from your mobile device by installing and logging into the x mobile app.\r\n \r\nIf you would prefer to receive greater details regarding your ticket updates in these messages please elect to receive encrypted emails."

My code:

                    descr = descr.replace("\r","\\r") 
                    descr = descr.replace("\n","\\n") 
                    descr = descr.encode('ascii', 'ignore')
                        gotdata = spitit[3]
                    except IndexError:

I am trying to remove the '\r\n\r\n' from before the text. I'm also not sure whay the 'b'' is appearing at the start of the text.

1 Answers

Brae On

There are a couple of issues here I think - the b problem is an encoding issue with your source data since it is being interpreted as byte data. You need to encode the string properly - I think the below should do it

descr = item.body.encode('utf-8')

The newlines appearing in the web app is a bit more complex. Firstly you are escaping the special characters with the replace calls, but if they are escaped they are read as strings not special characters. Also, \n\r is not a newline in web languages. Depending how you want it represented you may need to replace newlines with <br> tags or something like that to make it display properly, or just strip them out totally with descr.replace("\n","")