Python:如何将终端中打印的内容保存为变量以供以后比较?

发布于 2024-10-22 00:42:17 字数 751 浏览 2 评论 0原文

import imaplib
import pprint

IMAP_SERVER='imap.gmail.com'
IMAP_PORT=993

M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
rc, resp = M.login('[email protected]', 'password')
print rc, resp

M.select()
for msg_num in M.search(None, "UNDELETED")[1][0].split():
    msg = M.fetch('1', '(BODY.PEEK[TEXT])') 
    print msg[1][0][1][139:161]

M.close()         
M.logout()

我是 python 编程的新手,上面的 python 代码是我用于我想做的程序的代码。当我在终端中运行此命令时,我得到的响应是我已经验证了我的帐户,然后它显示字符 139 和 139 之间的消息。 161,在示例电子邮件中如下:

这只是一个测试...

这在终端中打印出来。我想做的就是将此打印输出与其他内容进行比较。例如:如果a=b则x。我想做的是,如果语句为真则向串口发送信号。

感谢所有帮助,我们感谢并期待任何帮助......

import imaplib
import pprint

IMAP_SERVER='imap.gmail.com'
IMAP_PORT=993

M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
rc, resp = M.login('[email protected]', 'password')
print rc, resp

M.select()
for msg_num in M.search(None, "UNDELETED")[1][0].split():
    msg = M.fetch('1', '(BODY.PEEK[TEXT])') 
    print msg[1][0][1][139:161]

M.close()         
M.logout()

I'm a new beginner in python programming and the above python code is one that I'm using for a program I want to do. When I run this in a terminal I get the response that I have authenticated my account and then it displays the message between characters 139 & 161, which is the following in the example email:

This is just a test...

This is printed out in the terminal. What I want to do is take this printout and compare it to something else. For example: if a=b then x. What I want to do is that if the statement is true to send a signal to the serial port.

Any help is appreciated and anticipated thanks to all that help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-10-29 00:42:17

只需在打印之前将消息分配给一个变量即可:

for msg_num in M.search(None, "UNDELETED")[1][0].split():
    msg = M.fetch('1', '(BODY.PEEK[TEXT])') 
    a = msg[1][0][1][139:161]
    print a

# later..
if a == 'this is just a test...':
   # your code here
   pass

显然,您应该使用比“a”更有意义的变量名称。

Simply assign the message to a variable before you print it:

for msg_num in M.search(None, "UNDELETED")[1][0].split():
    msg = M.fetch('1', '(BODY.PEEK[TEXT])') 
    a = msg[1][0][1][139:161]
    print a

# later..
if a == 'this is just a test...':
   # your code here
   pass

Obviously, you should use a more meaningful variable name than "a".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文