Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting string and removing whitespace Python

I would like to split a String by comma ',' and remove whitespace from the beginning and end of each split.

For example, if I have the string:

"QVOD, Baidu Player"

I would like to split and strip to:

['QVOD', 'Baidu Player']

Is there an elegant way of doing this? Possibly using a list comprehension?

like image 780
Daniel Pilch Avatar asked Jan 21 '14 14:01

Daniel Pilch


1 Answers

Python has a spectacular function called split that will keep you from having to use a regex or something similar. You can split your string by just calling my_string.split(delimiter)

After that python has a strip function which will remove all whitespace from the beginning and end of a string.

[item.strip() for item in my_string.split(',')]

Benchmarks for the two methods are below:

>>> import timeit
>>> timeit.timeit('map(str.strip, "QVOD, Baidu Player".split(","))', number=100000)
0.3525350093841553
>>> timeit.timeit('map(stripper, "QVOD, Baidu Player".split(","))','stripper=str.strip', number=100000)
0.31575989723205566
>>> timeit.timeit("[item.strip() for item in 'QVOD, Baidu Player'.split(',')]", number=100000)
0.246596097946167

So the list comp is about 33% faster than the map.

Probably also worth noting that as far as being "pythonic" goes, Guido himself votes for the LC. http://www.artima.com/weblogs/viewpost.jsp?thread=98196

like image 128
Slater Victoroff Avatar answered Sep 29 '22 12:09

Slater Victoroff