Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 split strip issues 'NonType'
#1
hello,

I am extremely new to python so bare with me. I am using a product that requires the use of python for custom functionality.

I have an automation tool that is ingesting and reading an email. I need this email to "split" at a specified string. That string is 'Classification:' however, Classification may not always be in the email to be split at, sometimes the string may be 'Sent with BlackBerry Work'. The string I am working with now is:

splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Classification:') if "Classification:" in filtered_artifacts_item_1_0 else (filtered_artifacts_item_1_0, "Sent with BlackBerry Work")[0].strip()
When I run this, I receive this:
, line 37, in splitresponse
AttributeError: 'list' object has no attribute 'strip'

any help would be appreciated, I basically need the email to split at either "Classification:" or "Sent with BlackBerry Work" it will always be one or the other present, never both.

Thank you,
Quote
#2
That line is too long IMHO. I would do it like this:

if "Classification:" in filtered_artifacts_item_1_0:
    splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Classification:')  
else:
    (filtered_artifacts_item_1_0, "Sent with BlackBerry Work")[0].strip()
From this code, it appears filtered_artifacts_item_1_0 is a list. In the fourth line there, you put it in a tuple with "Sent with BlackBerry Work". Then you get the [0] index of that tuple, which is filtered_artifacts_item_1_0. Then you try to strip that, which causes an error, because (as the error says) you can't strip a list.

My guess is that you mean to do something else when you make that tuple? If that's right, what are you trying to do?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Oct-07-2019, 04:56 PM)ichabod801 Wrote: That line is too long IMHO. I would do it like this:

if "Classification:" in filtered_artifacts_item_1_0:
    splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Classification:')  
else:
    (filtered_artifacts_item_1_0, "Sent with BlackBerry Work")[0].strip()
From this code, it appears filtered_artifacts_item_1_0 is a list. In the fourth line there, you put it in a tuple with "Sent with BlackBerry Work". Then you get the [0] index of that tuple, which is filtered_artifacts_item_1_0. Then you try to strip that, which causes an error, because (as the error says) you can't strip a list.

My guess is that you mean to do something else when you make that tuple? If that's right, what are you trying to do?

The input data is an email, we are wanting to strip out everything except 2 pieces of data from this email a Yes/No and a unique ID value, the body of the email will begin with either Yes or No, the goal is to split at a string that occurs after the Yes or No in the email body. The second piece of data will be the unique ID value, this will come after a string of "*****************" towards the end of the email, followed by the unique ID value.

The reason we are wanting to split is because there are two values we are trying to capture out of the email. the first being the Yes or No at the beginning, so the first "split" will be at either "Classification:" or "Sent with BlackBerry Work" to capture the Yes or No from the first position in the body of the email. The second split later after this first split is done, we have a second split occurring later in the body of the email after "*****************", we are wanting to strip everything left of this value and only retain the value after it (the unique ID value).

Example emails below, the "filtered_artifacts_item_1_0 is the email message itself:

email example 1):

Yes

Sent with BlackBerry Work
(http://www.blackberry.com)
___________________________________________________________________
From:
Date:
To:
Cc:
Subject:

*****************
BA2337C9-9E57-4BED-A984-8B28EFA6B403@@notable@@47e96bf35r92eabc107470cee79f27e9


email example 2):

Yes

Classification: Internal
___________________________________________________________________
From:
Date:
To:
Cc:
Subject:

*****************
BA2337C9-9E57-4BED-A984-8B28EFA6B403@@notable@@47e96bf35r92eabc107470cee79f27e9
Quote
#4
So if 'Classification' is in the email, you want to split by that, otherwise split by 'Sent by BlackBerry Work'? In that case it's just:

if "Classification:" in filtered_artifacts_item_1_0:
    splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Classification:')  
else:
    splitresponse__response_value = filtered_artifacts_item_1_0[0].split("Sent with BlackBerry Work")
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
(Oct-07-2019, 05:44 PM)ichabod801 Wrote: So if 'Classification' is in the email, you want to split by that, otherwise split by 'Sent by BlackBerry Work'? In that case it's just:

if "Classification:" in filtered_artifacts_item_1_0:
    splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Classification:')  
else:
    splitresponse__response_value = filtered_artifacts_item_1_0[0].split("Sent with BlackBerry Work")

Thank you, it accepted the syntax, still getting a NonType error but I'm going to have a fresh message sent to confirm it's just not ingesting a blank form.

Do you see any issues with my adding a third split? Also, I added [0].strip() to the end of each splitresponse as I want it to strip out the remainder of the email body. Correct?

   if "Classification:" in filtered_artifacts_item_1_0: 
        splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Classification:')[0].strip() 
    else: splitresponse__response_value = filtered_artifacts_item_1_0[0].split('Sent with BlackBerry Work')[0].strip()
    splitresponse__event_id = filtered_artifacts_item_1_0[0].split('*****************')[1].strip()
Quote
#6
That should work, assuming there is something there to strip.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#7
(Oct-07-2019, 08:47 PM)ichabod801 Wrote: That should work, assuming there is something there to strip.

Thank you again
Quote
#8
followup question.

I am going a different path now, still having issues with nontype. I am wondering how do I strip out a string that is on two lines?

This is what I want to strip:
Sent with BlackBerry Work
(http://www.blackberry.com)

right now my python is
filtered_artifacts_item_1_0[0].strip('Sent with BlackBerry Work (www.blackberry.com)')
I don't think this will work, since the URL is on the next line below "sent with blackberry for work"
Quote
#9
A newline character in python is '\n'. You can put that in the string instead of the space after 'Work'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  removing spaces/tabs after used .strip() zarize 0 114 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 547 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 344 Apr-15-2019, 03:14 AM
Last Post: ineuw
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 587 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE
  Why doesn't strip work? cybervigilante 5 745 Dec-27-2018, 02:21 AM
Last Post: metulburr
  Python with LED light strip question. Ostromaj 5 746 Aug-07-2018, 09:06 PM
Last Post: Ostromaj
  need algorithm to strip non-ascii characters from LONG csv file hereathome 3 1,396 Jan-12-2018, 02:04 AM
Last Post: hereathome
  Best way to strip() after split() k357k9 11 3,482 Nov-03-2017, 03:39 PM
Last Post: k357k9

Forum Jump:


Users browsing this thread: 1 Guest(s)