1

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=item.body
                    descr = descr.replace("\r","\\r") 
                    descr = descr.replace("\n","\\n") 
                    descr = descr.encode('ascii', 'ignore')
                    try:
                        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.

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
1 2
  • 11
  • 1
  • Try `descr= descr.split('b', 1)[1]` to remove the 1st `b` – Rahul Agarwal May 13 '19 at 14:33
  • 1
    What is your problem? – dome May 13 '19 at 14:34
  • 1
    The preceding `b` usually occurs if a byte array is printed – Cubimon May 13 '19 at 14:35
  • 2
    There's probably a better way to get rid of the `b` by simply not putting it there in the first place, but that's not something we can tell from the provided code. – glibdud May 13 '19 at 14:35
  • 1
    You have a decoding issue on read of your text, but without more info it's impossible to say what you're doing wrong. – thebjorn May 13 '19 at 14:36
  • Your replace code seems to be escaping the special characters with double backslashes - if they are escaped they will appear on screen instead of being interpreted as newlines etc. – Brae May 13 '19 at 14:37
  • 2
    Possible duplicate of [How do I get rid of the b-prefix in a string in python?](https://stackoverflow.com/questions/41918836/how-do-i-get-rid-of-the-b-prefix-in-a-string-in-python) – MooingRawr May 13 '19 at 14:39
  • From the example in the question, the `b` is _inside_ the string (`'b"hello world"'`). This means at some point someone has called `str` on a `bytes` instance instead of decoding. If your code is responsible for this then the duplicate target suggests the correct course of action. – snakecharmerb May 13 '19 at 14:42

1 Answers1

0

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","")

Brae
  • 484
  • 1
  • 3
  • 15