python用split拆分多次_如何在Python中拆分一次或多次分隔符?

I have a formatted string from a log file, which looks like:

我有一個日志文件中的格式化字符串,如下所示:

>>> a="test result"

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) cleared some unnecessary values:

split(DELIMITER,COUNT)清除了一些不必要的值:

>>> a.split(" ",1)

['test', ' result']

This helped - but of course, I really need:

這有幫助 - 但當然,我真的需要:

['test', 'result']

I can use split() followed by map + strip(), but I wondered if there is a more Pythonic way to do it.

我可以使用split()后跟map + strip(),但我想知道是否有更多的Pythonic方法來實現它。

Thanks,

Adam

UPDATE: Such a simple solution! Thank you all.

更新:這么簡單的解決方案!謝謝你們。

6 个解决方案

#1

43

Just do not give any delimeter?

只是不給任何分界儀?

>>> a="test result"

>>> a.split()

['test', 'result']

#2

27

>>> import re

>>> a="test result"

>>> re.split(" +",a)

['test', 'result']

>>> a.split()

['test', 'result']

#3

17

Just this should work:

這應該工作:

a.split()

Example:

>>> 'a b'.split(' ')

['a', '', '', '', '', '', 'b']

>>> 'a b'.split()

['a', 'b']

從文檔:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

如果未指定sep或為None,則應用不同的拆分算法:連續空格的運行被視為單個分隔符,如果字符串具有前導或尾隨空格,則結果將在開頭或結尾處不包含空字符串。因此,將空字符串或僅由空格組成的字符串拆分為無分隔符將返回[]。

#4

4

Any problem with simple a.split()?

簡單的a.split()有什么問題嗎?

#5

1

If you want to split by 1 or more occurrences of a delimiter and don't want to just count on the default split() with no parameters happening to match your use case, you can use regex to match the delimiter. The following will use one or more occurrences of . as the delimiter:

如果要分割一個或多個分隔符,並且不希望僅依賴默認的split()而沒有參數匹配您的用例,則可以使用正則表達式來匹配分隔符。以下將使用一次或多次。作為分隔符:

s = 'a.b....c......d.ef...g'

sp = re.compile('\.+').split(s)

print(sp)

which gives:

['a', 'b', 'c', 'd', 'ef', 'g']

#6

0

Just adding one more way, more useful in cases where delimiter is different from space, and s.split() will not work.

只需添加一種方法,在分隔符與空格不同的情況下更有用,而s.split()將不起作用。

like str = "Python,is,,more,,,,,flexible".

喜歡str =“Python,is ,, more ,,,,,靈活”。

In [27]: s = "Python is more flexible"

In [28]: str_list = list(filter(lambda x: len(x) > 0, s.split(" ")))

In [29]: str_list

Out[29]: ['Python', 'is', 'more', 'flexible']

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值