【问题标题】:If I could not iterate through Match.object, what should I do instead?如果我无法遍历 Match.object,我应该怎么做?
【发布时间】:2021-02-25 13:30:02
【问题描述】:

我正在为一项功能而苦苦挣扎。它是scraping Outlook 的功能,它找到一个特定的单词“Number1”并选择该单词附近的数字。其中一些数字以“0”开头,我想将其更改为“32”而不是“0”并保存为列表。

但我无法遍历 Match.object,而且我不知道如何实现我的目标的任何其他可能性。

这是我尝试过的:

def get_number(file):
   try:
       body = file.body
       matches = re.finditer(r"Number1:\s(.*)$", body, re.MULTILINE)
       list_of_numbers = []
       for match in matches:
             for i in match.group(1):
                  if i[0] == 0:
                      list_of_numbers.append("32" + i[1:])
        return list_of_numbers
      

   except Exception as e:
       print(e)

这是一个典型的电子邮件示例:

Subject: Test1

Hi,
You got a new answer from user Alex. 

Code: alex123fj
Number1: 0611111111
Number2: 1020
Number3: 3032

【问题讨论】:

    标签: python outlook match


    【解决方案1】:

    您的正则表达式只会找到 Number1: .. 但可能有多个以 0 开头的数字,如您在问题中所述。这是获得所需输出的方法:

    import re
    
    body = """
    Subject: Test1
    
    Hi,
    You got a new answer from user Alex. 
    
    Code: alex123fj
    Number1: 0611111111
    Number2: 1020
    Number3: 3032
    """
    
    matches = re.findall(r"^Number.*$", body, re.MULTILINE)
    # -> [Number1: 0611111111, Number2: 1020, Number3: 3032]
    
    # getting only numbers from that list
    nums = [ch for match in matches for ch in match.split() if ch.isdigit()]
    # -> ['0611111111', '1020', '3032']
    
    list_of_numbers = ['32' + i[1:] for i in nums if i[0] == '0']
    # -> ['32611111111']
    

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2020-11-26
      • 1970-01-01
      • 2018-01-16
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      相关资源
      最近更新 更多