用逗号分割并在 Python 中去除空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4071396/
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
Split by comma and strip whitespace in Python
提问by Mr_Chimp
I have some python code that splits on comma, but doesn't strip the whitespace:
我有一些以逗号分隔的 python 代码,但没有去除空格:
>>> string = "blah, lots , of , spaces, here "
>>> mylist = string.split(',')
>>> print mylist
['blah', ' lots ', ' of ', ' spaces', ' here ']
I would rather end up with whitespace removed like this:
我宁愿像这样删除空格:
['blah', 'lots', 'of', 'spaces', 'here']
I am aware that I could loop through the list and strip() each item but, as this is Python, I'm guessing there's a quicker, easier and more elegant way of doing it.
我知道我可以遍历 list 和 strip() 每个项目,但是,因为这是 Python,我猜有一种更快、更简单、更优雅的方法来做到这一点。
采纳答案by Sean Vieira
Use list comprehension -- simpler, and just as easy to read as a forloop.
使用列表理解——更简单,就像for循环一样容易阅读。
my_string = "blah, lots , of , spaces, here "
result = [x.strip() for x in my_string.split(',')]
# result is ["blah", "lots", "of", "spaces", "here"]
See:Python docs on List Comprehension
A good 2 second explanation of list comprehension.
回答by user470379
map(lambda s: s.strip(), mylist)would be a little better than explicitly looping. Or for the whole thing at once: map(lambda s:s.strip(), string.split(','))
map(lambda s: s.strip(), mylist)会比显式循环好一点。或者一次性完成整个事情:map(lambda s:s.strip(), string.split(','))
回答by user489041
Just remove the white space from the string before you split it.
在拆分字符串之前,只需从字符串中删除空格。
mylist = my_string.replace(' ','').split(',')
回答by Brad Montgomery
I know this has already been answered, but if you end doing this a lot, regular expressions may be a better way to go:
我知道这已经得到了回答,但如果你经常这样做,正则表达式可能是一个更好的方法:
>>> import re
>>> re.sub(r'\s', '', string).split(',')
['blah', 'lots', 'of', 'spaces', 'here']
The \smatches any whitespace character, and we just replace it with an empty string ''. You can find more info here: http://docs.python.org/library/re.html#re.sub
将\s匹配任何空白字符,我们只是用一个空字符串替换它''。您可以在此处找到更多信息:http: //docs.python.org/library/re.html#re.sub
回答by tbc0
Split using a regular expression. Note I made the case more general with leading spaces. The list comprehension is to remove the null strings at the front and back.
使用正则表达式拆分。请注意,我使用前导空格使案例更通用。列表推导就是去掉前后的空字符串。
>>> import re
>>> string = " blah, lots , of , spaces, here "
>>> pattern = re.compile("^\s+|\s*,\s*|\s+$")
>>> print([x for x in pattern.split(string) if x])
['blah', 'lots', 'of', 'spaces', 'here']
This works even if ^\s+doesn't match:
即使^\s+不匹配,这也有效:
>>> string = "foo, bar "
>>> print([x for x in pattern.split(string) if x])
['foo', 'bar']
>>>
Here's why you need ^\s+:
这就是你需要 ^\s+ 的原因:
>>> pattern = re.compile("\s*,\s*|\s+$")
>>> print([x for x in pattern.split(string) if x])
[' blah', 'lots', 'of', 'spaces', 'here']
See the leading spaces in blah?
看到 blah 中的前导空格了吗?
Clarification: above uses the Python 3 interpreter, but results are the same in Python 2.
说明:以上使用 Python 3 解释器,但结果与 Python 2 相同。
回答by Sean
I came to add:
我来补充:
map(str.strip, string.split(','))
map(str.strip, string.split(','))
but saw it had already been mentioned by Jason Orendorff in a comment.
但是看到 Jason Orendorff 在评论中已经提到了它。
Reading Glenn Maynard's comment in the same answer suggesting list comprehensions over map I started to wonder why. I assumed he meant for performance reasons, but of course he might have meant for stylistic reasons, or something else (Glenn?).
阅读 Glenn Maynard 在同一个答案中的评论,建议对地图进行列表理解,我开始想知道为什么。我假设他是出于性能原因,但当然他可能是出于风格原因或其他原因(格伦?)。
So a quick (possibly flawed?) test on my box applying the three methods in a loop revealed:
因此,对我的盒子进行快速(可能有缺陷?)测试,在循环中应用三种方法显示:
[word.strip() for word in string.split(',')]
$ time ./list_comprehension.py
real 0m22.876s
map(lambda s: s.strip(), string.split(','))
$ time ./map_with_lambda.py
real 0m25.736s
map(str.strip, string.split(','))
$ time ./map_with_str.strip.py
real 0m19.428s
making map(str.strip, string.split(','))the winner, although it seems they are all in the same ballpark.
成为map(str.strip, string.split(','))赢家,尽管他们似乎都在同一个球场上。
Certainly though map (with or without a lambda) should not necessarily be ruled out for performance reasons, and for me it is at least as clear as a list comprehension.
当然,出于性能原因,不一定要排除 map(有或没有 lambda),对我来说,它至少与列表理解一样清晰。
Edit:
编辑:
Python 2.6.5 on Ubuntu 10.04
Ubuntu 10.04 上的 Python 2.6.5
回答by Parikshit Pandya
s = 'bla, buu, jii'
sp = []
sp = s.split(',')
for st in sp:
print st
回答by Dannid
re(as in regular expressions) allows splitting on multiple characters at once:
re(如在正则表达式中)允许一次拆分多个字符:
$ string = "blah, lots , of , spaces, here "
$ re.split(', ',string)
['blah', 'lots ', ' of ', ' spaces', 'here ']
This doesn't work well for your example string, but works nicely for a comma-space separated list. For your example string, you can combine the re.split power to split on regex patternsto get a "split-on-this-or-that" effect.
这不适用于您的示例字符串,但适用于逗号空格分隔的列表。对于您的示例字符串,您可以结合 re.split 功能来拆分正则表达式模式以获得“拆分此或彼”效果。
$ re.split('[, ]',string)
['blah',
'',
'lots',
'',
'',
'',
'',
'of',
'',
'',
'',
'spaces',
'',
'here',
'']
Unfortunately, that's ugly, but a filterwill do the trick:
不幸的是,这很丑陋,但是filter可以解决问题:
$ filter(None, re.split('[, ]',string))
['blah', 'lots', 'of', 'spaces', 'here']
Voila!
瞧!
回答by Zieng
import re
result=[x for x in re.split(',| ',your_string) if x!='']
this works fine for me.
这对我来说很好用。
回答by DJbigpenis
map(lambda s: s.strip(), mylist)would be a little better than explicitly looping.
Or for the whole thing at once:
map(lambda s: s.strip(), mylist)会比显式循环好一点。
或者一次性完成整个事情:
map(lambda s:s.strip(), string.split(','))
That's basically everything you need.
这基本上就是你需要的一切。

