Python Forum
Best way to strip() after split()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to strip() after split()
#1
data = 'Red Duck | $125 | New | Ready | Very Good'
product, price, condition, available, rating = data.split('|')
Is there an easy way to strip leading and trailing spaces with the strip() function
without having to do each item separately?
Reply
#2
map and strip from string module
from string import strip
data = 'Red Duck | $125 | New | Ready | Very Good'
product, price, condition, available, rating = map(strip, data.split('|'))
map and lambda
data = 'Red Duck | $125 | New | Ready | Very Good'
product, price, condition, available, rating = map(lambda s:s.strip(), data.split('|'))
using list comprehension
data = 'Red Duck | $125 | New | Ready | Very Good'
product, price, condition, available, rating = [s.strip() for s in data.split('|')]
Reply
#3
You can use strip on same line:
>>> data = ' Red Duck | $125 | New | Ready | Very Good '.strip()
>>> data
'Red Duck | $125 | New | Ready | Very Good'
>>>
Reply
#4
Also split at  ' | ' if consistent

data = 'Red Duck | $125 | New | Ready | Very Good'
product, price, condition, available, rating = data.split(' | '))
Reply
#5
Thanks to buran & Larz60+ for the quick replies.
Let me work thru them and see if I have any more questions.
Reply
#6
buran - your #1 gives me a "Import Error: cannot import name 'strip'".
If I use import string & string.strip, it gives me a "AttributeError; module 'string' has no attribute "strip'".
Is this an old way of doing this that has been done away with?
Your #2 & #3 work just fine.
Reply
#7
It has been removed with Python 3. I guess the cause was code duplication. You can use instead str.strip.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
The str object has strip method. Any string is an str object so: "any string".strip()

In [1]: s = "a string"

In [2]: type(s)
Out[2]: str

In [3]: dir(s)
Out[3]: 
# I've removed the __dunder__  functions to shorten the output
['capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',                # here it is. 
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [4]: type("another string")
Out[4]: str

In [5]: "_strip it_".strip('_') # without an argument stip() will strip the whitespaces -  ' \t\n\r\x0b\x0c'
Out[5]: 'strip it'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Dead_EyE is right in python 3 you can use str.strip
 
Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'Red Duck | $125 | New | Ready | Very Good'
>>> product, price, condition, available, rating = map(str.strip, data.split('|'))
>>> print(product, rating)
Red Duck Very Good
>>> 
My bad, I should have mentioned it's python2 or leave it out
Reply
#10
buran - If I use ' | ' (with the spaces) in the interpreter, it gives me an error. If I use it
in a program, it runs, but I end up with no data extracted. This is very strange.

BTW, how did you get that little box around ' | '?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,790 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,151 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Can't strip extra characters from Data Canflyguy 7 1,813 Jan-10-2022, 02:16 PM
Last Post: Canflyguy
  strip() pprod 8 3,373 Feb-16-2021, 01:11 PM
Last Post: buran
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,702 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  split strip issues 'NonType' casacafe 8 3,762 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,554 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,226 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  strip space from end of a row of text ineuw 4 2,823 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 4,372 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020