Sunday, August 26, 2018

regex - 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?


Answer



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


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...