如何在Python中除以1或多次出现分隔符? [英] How can I split by 1 or more occurrences of a delimiter in Python?
问题描述
我有一个来自日志文件的格式化字符串,如下所示:
I have a formatted string from a log file, which looks like:
>>> a="test result"
也就是说,测试和结果被一些空格分开-可能是使用格式化的字符串创建的,该字符串为 test
提供了一些恒定的间距。
That is, the test and the result are split by some spaces - it was probably created using formatted string which gave test
some constant spacing.
简单的拆分不会成功:
Simple splitting won't do the trick:
>>> a.split(" ")
['test', '', '', '', ... '', '', '', '', '', '', '', '', '', '', '', 'result']
split(DELIMITER,COUNT)
清除了一些不必要的值:
split(DELIMITER, COUNT)
cleared some unnecessary values:
>>> a.split(" ",1)
['test', ' result']
这很有帮助-但是我当然需要:
This helped - but of course, I really need:
['test', 'result']
我可以使用 split()
后跟 map
+ strip()
,但我想知道是否还有更Python化的方法。
I can use split()
followed by map
+ strip()
, but I wondered if there is a more Pythonic way to do it.
谢谢
亚当
更新:简单的解决方案!谢谢大家。
UPDATE: Such a simple solution! Thank you all.
推荐答案
只是不给定距离?
>>> a="test result"
>>> a.split()
['test', 'result']
这篇关于如何在Python中除以1或多次出现分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!