Python按空格拆分字符串并去除换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25253120/
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-18 19:57:31  来源:igfitidea点击:

Python split string by space and strip newline char

pythonstringsplit

提问by J Freebird

I have a string in python that looks like:

我在 python 中有一个字符串,看起来像:

d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00 
ff ff 00 00 01 00 00 00  00 00 00 00 00 00 00 00 
36 00 00 00 36 00 00 00  00 1c 23 10 f8 f1 00 1b 
17 01 10 20 08 00 45 00  00 28 df 27 40 00 80 06 
2b 87 c7 08 1a 0a 0a 05  05 0a 5c ea 5c ea c2 1f 

There're many more lines that I skipped. I want to put each of the numbers in a list. When I use .split(), it returns me a list of not just numbers, but also spaces and '\n's, because there're two spaces in the middle of the matrix and there're newlines chars at the end of each line. So I got:

我跳过了更多的行。我想把每个数字放在一个列表中。当我使用 .split() 时,它不仅返回数字列表,还返回空格和 '\n's 的列表,因为矩阵中间有两个空格,每行末尾都有换行符. 所以我得到了:

['d4', 'c3', 'b2', 'a1', '02', '00', '04', '00', '', '00', …, '\nff', 'ff', '00'…]

I just want the numbers to be in the list, not anything else, anybody knows how to do it? Thanks for your time in advance.

我只希望数字出现在列表中,而不是其他任何东西,有人知道怎么做吗?提前感谢您的时间。

采纳答案by TheSoundDefense

If you use .split(" "), then your program will split on every single space, and not on any other whitespace. If you use .split()instead, the program will take into account multiple spaces, newlines, tabs and all other forms of whitespace. That should get you what you're looking for.

如果您使用.split(" "),那么您的程序将在每个空格上拆分,而不是在任何其他空格上。如果您.split()改为使用,程序将考虑多个空格、换行符、制表符和所有其他形式的空格。那应该让你得到你正在寻找的东西。

>>> teststr = "a   v w   ef sdv   \n   wef"
>>> print teststr
a   v w   ef sdv   
   wef
>>> teststr.split()
['a', 'v', 'w', 'ef', 'sdv', 'wef']
>>> teststr.split(" ")
['a', '', '', 'v', 'w', '', '', 'ef', 'sdv', '', '', '\n', '', '', 'wef']

回答by mlinsenbard

The python stringdocumentation has a huge list of things you can do to strings.

Python字符串文档中,有大量的事情可以做,以字符串列表。

What's odd is that the .split()is not eliminating all of your whitespace when, as mentioned above by TheSoundDefense, it should.

奇怪的是.split(),正如 TheSoundDefense 上面提到的那样,它并没有消除所有的空白。

To get rid of the newlines, try using the .replace(<target>,<replacement>)method like so:

要摆脱换行符,请尝试使用如下.replace(<target>,<replacement>)方法:

a = '11 11 11 11  11 11 11 11 \n22 22 22 22  22 22 22 22 \n'
b = a.replace('\n',' ')
c = b.split()
print c
>>> ['11', '11', '11', '11', '11', '11', '11', '11', '22', '22', '22', '22', '22', '22', '22', '22']

回答by HAL

Using split()without any arguments will split the contents on any whitespaces and also group together several whitespaces.

split()不带任何参数使用将拆分任何空格上的内容,并将几个空格组合在一起。

Here is an example:

下面是一个例子:

s = """d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00 
ff ff 00 00 01 00 00 00  00 00 00 00 00 00 00 00 
36 00 00 00 36 00 00 00  00 1c 23 10 f8 f1 00 1b 
17 01 10 20 08 00 45 00  00 28 df 27 40 00 80 06 
2b 87 c7 08 1a 0a 0a 05  05 0a 5c ea 5c ea c2 1f"""

data = s.split()

In this case, datawill look like this:

在这种情况下,data将如下所示:

['d4', 'c3', 'b2', 'a1', '02', '00', '04', '00', '00', '00', '00', '00', '00', '00', '00', '00', 'ff', 'ff', '00', '00', '01', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '36', '00', '00', '00', '36', '00', '00', '00', '00', '1c', '23', '10', 'f8', 'f1', '00', '1b', '17', '01', '10', '20', '08', '00', '45', '00', '00', '28', 'df', '27', '40', '00', '80', '06', '2b', '87', 'c7', '08', '1a', '0a', '0a', '05', '05', '0a', '5c', 'ea', '5c', 'ea', 'c2', '1f']