Python 仅将字符串拆分为两部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50848764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:37:05  来源:igfitidea点击:

Split string into two parts only

pythonsplit

提问by Abhishek Velankar

I have a string

我有一个字符串

s = 'abcd qwrre qwedsasd zxcwsacds'

I want to split any string in only two parts at the first occurrence of a whitespace. i.e. a='abcd'and b='qwrre qwedsasd zxcwsacds'

我想在第一次出现空格时将任何字符串分成两部分。a='abcd'b='qwrre qwedsasd zxcwsacds'

If I use a, b=split(' ')it gives me an error because there are too many values to unpack.

如果我使用a, b=split(' ')它会给我一个错误,因为有太多的值需要解包。

回答by TMK

You could use a,b = split(' ', 1).

你可以使用a,b = split(' ', 1).

The second argument 1is the maximum number of splits that would be done.

第二个参数1是将完成的最大拆分数。

s = 'abcd efgh hijk'
a,b = s.split(' ', 1)
print(a) #abcd
print(b) #efgh hijk

For more information on the string split function, see str.splitin the manual.

有关字符串拆分功能的更多信息,请参阅str.split手册中的

回答by ajxs

From the Python docs

来自Python 文档

str.split(sep=None, maxsplit=-1)

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

str.split(sep=None, maxsplit=-1)

返回字符串中单词的列表,使用 sep 作为分隔符字符串。如果给出了 maxsplit,则最多完成 maxsplit 次分割(因此,列表最多有 maxsplit+1 个元素)。如果未指定 maxsplit 或 -1,则对拆分的数量没有限制(进行所有可能的拆分)。

'1 2 3'.split(maxsplit=1)
# ['1', '2 3']

回答by constt

You can use a standard string method partitionwhich searches for a given separator and returns a 3-tuple consisting of string part before it, the separator itself, and the part after it.

您可以使用标准字符串方法partition来搜索给定的分隔符并返回一个 3 元组,它由前面的字符串部分、分隔符本身和后面的部分组成。

>>> s = 'abcd qwrre qwedsasd zxcwsacds'
>>> s.partition(' ')
('abcd', ' ', 'qwrre qwedsasd zxcwsacds')

回答by Zack Atama

You're missing one more parameter in your split, the number of occurrences, try this;

你在你的分割中又缺少一个参数,出现的次数,试试这个;

s='abcd qwrre qwedsasd zxcwsacds'
>>> a, b = s.split(' ', 1)
>>> print(a, b)

回答by JJ123

You can solve this problem by using python "star expression".

您可以通过使用python“星形表达式”来解决这个问题。

s='abcd qwrre qwedsasd zxcwsacds'
a = s.split()
first, *second = a
second = " ".join(second)

"first" takes the first element of the list and " *second" takes the remaining elements.

“first”取列表的第一个元素,“*second”取其余元素。

>>> first
'abcd'
>>> second
'qwrre', 'qwedsasd', 'zxcwsacds'

回答by U10-Forward

Try this:

尝试这个:

s = 'abcd qwrre qwedsasd zxcwsacds'
s1 = s.split()[0]
s2 = ' '.join(s.split()[1:])
print(s1)
print(s2)

Output:

输出:

abcd
qwrre qwedsasd zxcwsacds

Or:

或者:

new_s = ''.join([' s ' if i.isspace() else i for i in s])
a,b = new_s.replace(' s','',1).split(' s ')
print(a)
print(b)

Output:

输出:

abcd
qwrre qwedsasd zxcwsacds

Or even better split also for tabs and newlines:

或者甚至更好地拆分制表符和换行符:

a,b = s.split(None,1)
print(a)
print(b)

Output:

输出:

abcd
qwrre qwedsasd zxcwsacds

回答by Rajat Jain

1.You can split the string like this.

1.你可以像这样拆分字符串。

a = temp.substring(0, s.indexOf(' '));
b = temp.substring(s.indexOf(' ') + 1);

2.or you can do:

2.或者你可以这样做:

a = s[0: s.find(' ')]
b = s[s.find(' ') + 1: ]

3.

3.

a,b = s.split(' ', 1)
print(a)
print(b) 

回答by mroman

So, there's a second parameter you can pass as many have pointed out:

因此,正如许多人指出的那样,您可以传递第二个参数:

>>> a, b = "foo bar".split(' ', 1)

but nobody's pointing this out:

但没有人指出这一点:

>>> a, b = "foobar".split(' ', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

You don't want to be using this because it's unsafe unless you're guaranteed to have a string that has only the one space in it. It's better to split it and then check for how many splits you've got and then unpack them:

你不想使用它,因为它是不安全的,除非你保证有一个只有一个空格的字符串。最好将它拆分,然后检查您有多少个拆分,然后解压缩它们:

>>> parts = "foo bar".split(' ', 1)
>>> if len(parts) == 2:
>>>    a, b = parts 

回答by Michael Swartz

#!python2

s='abcd qwrre qwedsasd zxcwsacds'

s1 = s[0: s.find(' ')]
s2 = s[s.find(' ') + 1: ]

print s1
print s2
'''
abcd
qwrre qwedsasd zxcwsacds
'''