简体   繁体   English

Imaplib使用python搜索带有特殊字符的主题行

[英]Imaplib search for subject line with special characters using python

I'm trying to search special characters subject lines using 我正在尝试使用搜索特殊字符主题行

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "Report #160496147 : "AU report QTD date wise MediaOps" from Dhruv Sapra"')

but it's raising a bad request command error. 但它引发了错误的请求命令错误。

Any idea why its happening and any solution for it. 任何想法为什么会发生,以及任何解决方案。

the below solution is not working. 以下解决方案不起作用。

Python3 IMAP search special characters Python3 IMAP搜索特殊字符

Your problem is first that you have double quotes within your quoted string, which is a syntax error: 您的问题首先是在带引号的字符串中有双引号,这是一种语法错误:

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "Report #160496147 : "AU report QTD date wise MediaOps" from Dhruv Sapra"')

Try escaping your inner quotes: 尝试转义内部引号:

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "Report #160496147 : \"AU report QTD date wise MediaOps\" from Dhruv Sapra"')

Also note that how SEARCH actually works is implementation specific. 还要注意, SEARCH实际工作方式是特定于实现的。 Searching for a word or two usually works well, but searching for literal strings often does not, depending on how the server indexes it, if at all. 搜索一两个单词通常效果很好,但是搜索文字字符串通常效果不佳,这取决于服务器如何为它建立索引。 For example, the gmail server indexes words only, and searching for substrings or punctuation usually does not work. 例如,gmail服务器仅索引单词,而搜索子字符串或标点符号通常不起作用。 If you server is compatible, just searching for the report number with or without the # may or may not give you better results. 如果您的服务器兼容,则仅搜索报告编号(带或不带#)可能会或不会带来更好的结果。

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "#160496147"')
typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "160496147"')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM