小编bbu*_*net的帖子

在 Python 中使用正则表达式从文本中提取列表

我希望从以下字符串中提取元组列表:

text='''Consumer Price Index:
        +0.2% in Sep 2020

        Unemployment Rate:
        +7.9% in Sep 2020

        Producer Price Index:
        +0.4% in Sep 2020

        Employment Cost Index:
        +0.5% in 2nd Qtr of 2020

        Productivity:
        +10.1% in 2nd Qtr of 2020

        Import Price Index:
        +0.3% in Sep 2020

        Export Price Index:
        +0.6% in Sep 2020'''
Run Code Online (Sandbox Code Playgroud)

我在该过程中使用“import re”。

输出应类似于:[('Consumer Price Index', '+0.2%', 'Sep 2020'), ...]

我想使用 re.findall 函数来生成上述输出,到目前为止我有这个:

re.findall(r"(:\Z)\s+(%\Z+)(\Ain )", text)
Run Code Online (Sandbox Code Playgroud)

我先识别“:”之前的字符,然后识别“%”之前的字符,然后识别“in”之后的字符。

我真的不知道如何继续。任何帮助,将不胜感激。谢谢!

python regex text-mining

4
推荐指数
1
解决办法
1060
查看次数

标签 统计

python ×1

regex ×1

text-mining ×1