在 outlook body 中搜索文本,然后为文本所在的行创建一个变量
Search outlook body for text, then create a variable for the line the text is on
每天早上我都会通过电子邮件收到外汇交易量的现货数据,我想建立一个流程来搜索电子邮件正文中的两条数据并将它们保存为一个新变量,然后我可以参考稍后。
我有搜索我的电子邮件、根据日期排序并检查输入的数据是否存在于电子邮件中的过程,但由于数据包含在两个逗号之间的格式中,我不确定如何取出该数据并将其分配给一个新变量。
例如格式是这样的:
BWP/USD,0
CHF/AMDT,0
这是我到目前为止所取得的成就:
import win32com.client
import os
import time
import re
# change the ticker to the one you're looking for
FX_volume1 = "BWP/USD"
FX_volume2 = "CHF/AMD"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
# find spot data
for message in messages:
if message.subject.startswith("FX SPOT FIGURES"):
if FX_volume1 and FX_volume2 in message.body:
data = message.body
print(data)
else:
print('No data for', FX_volume1, 'or', FX_volume2, 'was found')
break
知道如何推进这项工作吗?
感谢任何 assistance/pointers
import win32com.client
import os
import time
import re
# change the ticker to the one you're looking for
FX_volume1 = "BWP/USD"
FX_volume2 = "CHF/AMD"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
# find spot data
for message in messages:
if message.subject.startswith("FX SPOT FIGURES"):
case1 = re.match(FX_volume1 + ",(\d*)", message.body)
case2 = re.match(FX_volume2 + ",(\d*)", message.body)
如果找到匹配,case(1 和 2)将是匹配对象,否则它们将是 None。要检索您的值,只需执行 val = case1.group(1)
。因此:
编辑:
if case1 not None:
FX_vol1_val = case1.group(1)
if case2 not None:
FX_vol2_val = case1.group(1)
有关匹配对象的更多信息:
https://docs.python.org/3/library/re.html#match-objects
如果您需要花车,请参阅以下内容 link:
Regular expression for floating point numbers
编辑 2:
您好,因为您无法让它工作,所以我快速尝试了一下,它对我来说适用于以下示例。只是为了添加到正则表达式符号,任何你放在括号 () 中的东西,如果模式匹配,括号之间的内容将被存储。
import re
my_text = "BWP/USD,1"
FX_pattern = "BWP/USD," # added the comma here for my convinience
my_match = re.match(FX_pattern, "(\d*)")
print("Group 0:", my_match.group(0))
print("Group 1:", my_match.group(1))
打印输出:
Group 0: BWP/USD,1
Group 1: 1
每天早上我都会通过电子邮件收到外汇交易量的现货数据,我想建立一个流程来搜索电子邮件正文中的两条数据并将它们保存为一个新变量,然后我可以参考稍后。
我有搜索我的电子邮件、根据日期排序并检查输入的数据是否存在于电子邮件中的过程,但由于数据包含在两个逗号之间的格式中,我不确定如何取出该数据并将其分配给一个新变量。
例如格式是这样的:
BWP/USD,0
CHF/AMDT,0
这是我到目前为止所取得的成就:
import win32com.client
import os
import time
import re
# change the ticker to the one you're looking for
FX_volume1 = "BWP/USD"
FX_volume2 = "CHF/AMD"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
# find spot data
for message in messages:
if message.subject.startswith("FX SPOT FIGURES"):
if FX_volume1 and FX_volume2 in message.body:
data = message.body
print(data)
else:
print('No data for', FX_volume1, 'or', FX_volume2, 'was found')
break
知道如何推进这项工作吗?
感谢任何 assistance/pointers
import win32com.client
import os
import time
import re
# change the ticker to the one you're looking for
FX_volume1 = "BWP/USD"
FX_volume2 = "CHF/AMD"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
# find spot data
for message in messages:
if message.subject.startswith("FX SPOT FIGURES"):
case1 = re.match(FX_volume1 + ",(\d*)", message.body)
case2 = re.match(FX_volume2 + ",(\d*)", message.body)
如果找到匹配,case(1 和 2)将是匹配对象,否则它们将是 None。要检索您的值,只需执行 val = case1.group(1)
。因此:
编辑:
if case1 not None:
FX_vol1_val = case1.group(1)
if case2 not None:
FX_vol2_val = case1.group(1)
有关匹配对象的更多信息: https://docs.python.org/3/library/re.html#match-objects
如果您需要花车,请参阅以下内容 link: Regular expression for floating point numbers
编辑 2:
您好,因为您无法让它工作,所以我快速尝试了一下,它对我来说适用于以下示例。只是为了添加到正则表达式符号,任何你放在括号 () 中的东西,如果模式匹配,括号之间的内容将被存储。
import re
my_text = "BWP/USD,1"
FX_pattern = "BWP/USD," # added the comma here for my convinience
my_match = re.match(FX_pattern, "(\d*)")
print("Group 0:", my_match.group(0))
print("Group 1:", my_match.group(1))
打印输出:
Group 0: BWP/USD,1
Group 1: 1