【问题标题】:Extract informations from a string in Python? [closed]从 Python 中的字符串中提取信息? [关闭]
【发布时间】:2016-07-11 10:53:14
【问题描述】:

我有一个字符串,我想只提取其中的一些信息。

例如我有这个:

'Won 3 Oscars. Another 80 wins & 121 nominations.'

我想拆分它以获得这样的列表:

['3 Oscars', '80 wins', '121 nominations']

在 Python 中我该怎么做?

谢谢

【问题讨论】:

  • 您应该定义提取信息的约束条件。数字后跟单词?每隔第二个和第三个元素?

标签: python regex


【解决方案1】:

一个数字,后跟一个空格,一个单词,然后是一个单词边界。应该这样做:

import re

s = 'Won 3 Oscars. Another 80 wins & 121 nominations.'
p = re.compile(r'\d+\s\w+\b')

print(p.findall(s))
# ['3 Oscars', '80 wins', '121 nominations']

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多