How can I split and parse a string in Python?

I am trying to break this line in python: 2.7.0_bf4fda703454

I want to break this line under the underscore _ so that I can use the value on the left side.

+93
python string split parsing
Apr 21 2018-11-21T00:
source share
3 answers

"2.7.0_bf4fda703454".split("_") a list of lines:

 In [1]: "2.7.0_bf4fda703454".split("_") Out[1]: ['2.7.0', 'bf4fda703454'] 

This breaks the line in each underscore. If you want it to stop after the first split, use "2.7.0_bf4fda703454".split("_", 1) .

If you know for sure that the string contains an underscore, you can even unpack LHS and RHS into separate variables:

 In [8]: lhs, rhs = "2.7.0_bf4fda703454".split("_", 1) In [9]: lhs Out[9]: '2.7.0' In [10]: rhs Out[10]: 'bf4fda703454' 

An alternative is to use partition() . The usage is similar to the last example, except that it returns three components instead of two. The main advantage is that this method does not fail if the string does not contain a delimiter.

+122
Apr 21 2018-11-21T00:
source share

Python string parsing

Divide the line into a space, get the list, show its type, print it:

 el@apollo:~/foo$ python >>> mystring = "What does the fox say?" >>> mylist = mystring.split(" ") >>> print type(mylist) <type 'list'> >>> print mylist ['What', 'does', 'the', 'fox', 'say?'] 

If you have two delimiters next to each other, an empty string is assumed:

 el@apollo:~/foo$ python >>> mystring = "its so fluffy im gonna DIE!!!" >>> print mystring.split(" ") ['its', '', 'so', '', '', 'fluffy', '', '', 'im', 'gonna', '', '', '', 'DIE!!!'] 

Divide the line by underlining and grab the 5th item in the list:

 el@apollo:~/foo$ python >>> mystring = "Time_to_fire_up_Kowalski's_Nuclear_reactor." >>> mystring.split("_")[4] "Kowalski's" 

Collapse multiple spaces into one

 el@apollo:~/foo$ python >>> mystring = 'collapse these spaces' >>> mycollapsedstring = ' '.join(mystring.split()) >>> print mycollapsedstring.split(' ') ['collapse', 'these', 'spaces'] 

When you do not pass the Python split method parameter, the docs say : "runs of consecutive spaces are treated as one separator, and the result will not contain empty lines at the beginning or at the end if the line has leading or trailing spaces."

Hold on to your boy boys, parse the regex:

 el@apollo:~/foo$ python >>> mystring = 'zzzzzzabczzzzzzdefzzzzzzzzzghizzzzzzzzzzzz' >>> import re >>> mylist = re.split("[am]+", mystring) >>> print mylist ['zzzzzz', 'zzzzzz', 'zzzzzzzzz', 'zzzzzzzzzzzz'] 

The regular expression "[am] +" means that lowercase letters a through m that occur one or more times are matched as a delimiter. re is the library to be imported.

Or, if you want to rob elements one at a time:

 el@apollo:~/foo$ python >>> mystring = "theres coffee in that nebula" >>> mytuple = mystring.partition(" ") >>> print type(mytuple) <type 'tuple'> >>> print mytuple ('theres', ' ', 'coffee in that nebula') >>> print mytuple[0] theres >>> print mytuple[2] coffee in that nebula 
+67
Jan 08
source share

If it will always be LHS / RHS separation, you can also use the partition method built into the strings. It returns a 3-tuple as (LHS, separator, RHS) if the separator is found, and (original_string, '', '') if the separator was not present:

 >>> "2.7.0_bf4fda703454".partition('_') ('2.7.0', '_', 'bf4fda703454') >>> "shazam".partition("_") ('shazam', '', '') 
+17
Apr 21 2018-11-21T00:
source share



All Articles