【发布时间】:2011-05-03 13:15:27
【问题描述】:
我有一些用逗号分隔的 python 代码,但没有去掉空格:
>>> string = "blah, lots , of , spaces, here "
>>> mylist = string.split(',')
>>> print mylist
['blah', ' lots ', ' of ', ' spaces', ' here ']
我宁愿像这样删除空格:
['blah', 'lots', 'of', 'spaces', 'here']
我知道我可以遍历 list 并 strip() 每个项目,但由于这是 Python,我猜有一种更快、更简单、更优雅的方法。
【问题讨论】:
标签: python whitespace strip