如何在Python中分割和解析字符串?

时间:2023-01-11 21:47:42

I am trying to split this string in python: 2.7.0_bf4fda703454

我正在尝试用python: 2.7.0_bf4fda703454分割这个字符串

I want to split that string on the underscore _ so that I can use the value on the left side.

我想要在下划线_上分割这个字符串,这样我就可以在左边使用这个值。

3 个解决方案

#1


106  

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

"2.7.0_bf4fda703454".split("_")给出字符串列表:

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

This splits the string at every underscore. If you want it to stop after the first split, use "2.7.0_bf4fda703454".split("_", 1).

这将在每个下划线处分割字符串。如果希望在第一次拆分后停止,请使用“2.7.0_bf4fda703454”。分割(“_”,1)。

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

如果您知道字符串包含下划线,您甚至可以将LHS和RHS解压缩到单独的变量中:

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 pointed out by @S.Lott is to use partition. The usage is similar to the last example, except that it returns three components instead of two. The principal advantage is that this method doesn't fail if the string doesn't contain the separator. This method, however, requires Python 2.5.

@S指出的另一种选择。Lott使用分区。这个用法与上一个示例类似,只是返回了三个组件而不是两个。主要优点是,如果字符串不包含分隔符,该方法不会失败。然而,这种方法需要Python 2.5。

#2


60  

Python string parsing walkthrough

Split a string on space, get a list, show its type, print it out:

在空间上分割一个字符串,得到一个列表,显示它的类型,打印出来:

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, empty string is assumed:

如果相邻有两个分隔符,则假定为空字符串:

el@apollo:~/foo$ python
>>> mystring = "its  so   fluffy   im gonna    DIE!!!"

>>> print mystring.split(" ")
['its', '', 'so', '', '', 'fluffy', '', '', 'im', 'gonna', '', '', '', 'DIE!!!']

Split a string on underscore and grab the 5th item in the list:

在下划线上分割一个字符串,获取列表中的第5项:

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 pass no parameter to Python's split method, the documentation states: "runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace".

当您不向Python的split方法传递任何参数时,文档声明:“连续空格的运行被视为一个单独的分隔符,如果字符串有前导或后导空格,则结果在开始或结束时将不包含空字符串”。

Hold onto your hats boys, parse on a regular expression:

保持你的帽子,男孩,解析一个正则表达式:

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

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

正则表达式“[a-m]+”表示出现一次或多次的小写字母a到m作为分隔符进行匹配。re是一个要导入的库。

Or if you want to chomp the items 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

#3


16  

If it's always going to be an even LHS/RHS split, you can also use the partition method that's built into strings. It returns a 3-tuple as (LHS, separator, RHS) if the separator is found, and (original_string, '', '') if the separator wasn't present:

如果它总是一个偶LHS/RHS分割,您也可以使用构建在字符串中的分区方法。如果找到分隔符,则返回一个3元组as (LHS, separator, RHS),如果没有分隔符,则返回(original_string, ", "):

>>> "2.7.0_bf4fda703454".partition('_')
('2.7.0', '_', 'bf4fda703454')

>>> "shazam".partition("_")
('shazam', '', '')

#1


106  

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

"2.7.0_bf4fda703454".split("_")给出字符串列表:

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

This splits the string at every underscore. If you want it to stop after the first split, use "2.7.0_bf4fda703454".split("_", 1).

这将在每个下划线处分割字符串。如果希望在第一次拆分后停止,请使用“2.7.0_bf4fda703454”。分割(“_”,1)。

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

如果您知道字符串包含下划线,您甚至可以将LHS和RHS解压缩到单独的变量中:

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 pointed out by @S.Lott is to use partition. The usage is similar to the last example, except that it returns three components instead of two. The principal advantage is that this method doesn't fail if the string doesn't contain the separator. This method, however, requires Python 2.5.

@S指出的另一种选择。Lott使用分区。这个用法与上一个示例类似,只是返回了三个组件而不是两个。主要优点是,如果字符串不包含分隔符,该方法不会失败。然而,这种方法需要Python 2.5。

#2


60  

Python string parsing walkthrough

Split a string on space, get a list, show its type, print it out:

在空间上分割一个字符串,得到一个列表,显示它的类型,打印出来:

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, empty string is assumed:

如果相邻有两个分隔符,则假定为空字符串:

el@apollo:~/foo$ python
>>> mystring = "its  so   fluffy   im gonna    DIE!!!"

>>> print mystring.split(" ")
['its', '', 'so', '', '', 'fluffy', '', '', 'im', 'gonna', '', '', '', 'DIE!!!']

Split a string on underscore and grab the 5th item in the list:

在下划线上分割一个字符串,获取列表中的第5项:

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 pass no parameter to Python's split method, the documentation states: "runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace".

当您不向Python的split方法传递任何参数时,文档声明:“连续空格的运行被视为一个单独的分隔符,如果字符串有前导或后导空格,则结果在开始或结束时将不包含空字符串”。

Hold onto your hats boys, parse on a regular expression:

保持你的帽子,男孩,解析一个正则表达式:

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

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

正则表达式“[a-m]+”表示出现一次或多次的小写字母a到m作为分隔符进行匹配。re是一个要导入的库。

Or if you want to chomp the items 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

#3


16  

If it's always going to be an even LHS/RHS split, you can also use the partition method that's built into strings. It returns a 3-tuple as (LHS, separator, RHS) if the separator is found, and (original_string, '', '') if the separator wasn't present:

如果它总是一个偶LHS/RHS分割,您也可以使用构建在字符串中的分区方法。如果找到分隔符,则返回一个3元组as (LHS, separator, RHS),如果没有分隔符,则返回(original_string, ", "):

>>> "2.7.0_bf4fda703454".partition('_')
('2.7.0', '_', 'bf4fda703454')

>>> "shazam".partition("_")
('shazam', '', '')