我想使用python接收电子邮件.到目前为止,我已经能够得到主题而不是身体.这是我一直在使用的代码:
import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('myusername')
pop_conn.pass_('mypassword')
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
print message['subject']
print message['body']
pop_conn.quit()
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我运行此代码时,它正确返回主题但不返回正文.因此,如果我发送一封主题为"Tester"的电子邮件和正文"This is a test message",它在IDLE中就会显示出来.
>>>>Tester >>>>None
Run Code Online (Sandbox Code Playgroud)
所以它似乎是准确评估主题而不是身体,我认为它在解析方法中是正确的吗?问题是我对这些库了解不足以弄清楚如何更改它以便它返回主题和正文.
所以我的问题是这个,我有一个看起来像这样的文件:
[SHIFT]this isrd[BACKSPACE][BACKSPACE] an example file[SHIFT]1
Run Code Online (Sandbox Code Playgroud)
这当然会转化为
' This is an example file!'
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法将原始内容解析为最终内容,以便[BACKSPACE]删除最后一个字符(包含空格)和多个退格将删除多个字符.这[SHIFT]对我来说并不重要.感谢您的帮助!
我有一个大的字符串文本文件,我想每117个字符拆分字符串并将下一个117个字符放在换行符上,依此类推,直到文件结束.
我试过这个:`s ="""我出于可见性原因删除了字符串"""space ="""
"""file = open('testoutput.txt','w')而s:print s [:10] output = output + s +""""
"""
s = s[10:]
Run Code Online (Sandbox Code Playgroud)
file.write(output)file.close()print"Done"`
但有一个问题,文件中的最终输出看起来像这个级联:`这[SHIFT] r [BACKSPACE]分子及其后代av [BACKSPACE] [BACKSPACE]因突变而变化
T]r[BACKSPACE]molecule and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations
ACE]molecule and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations
le and its descendants would av[BACKSPACE][BACKSPACE]vary because of mutations
descendants would av[BACKSPACE][BACKSPACE]vary because of mutations
ts would av[BACKSPACE][BACKSPACE]vary because of mutations
v[BACKSPACE][BACKSPACE]vary because of mutations
E][BACKSPACE]vary because of mutations
CE]vary because of mutations
cause of mutations
utations …Run Code Online (Sandbox Code Playgroud) 我有一个大约58k字的英语wlist.txt文件,其中一小段摘录如下:
aardvark
aardwolf
aaron
aback
abacus
abaft
abalone
abandon
abandoned
abandonment
abandons
abase
abased
abasement
Run Code Online (Sandbox Code Playgroud)
我想要做的是在列表中搜索程序并查看列表中是否包含单词,如果是,则打印单词.我的问题是,我写的代码将不断返回,不,这个词不在列表中,当我确定它是.我的代码看起来像这样,有人注意到任何错误吗?
match = 'aardvark'
f = 'wlist.txt'
success = False
try:
for word in open(f):
if word == match:
success = True
break
except IOError:
print f, "not found!"
if success:
print "The word has been found with a value of", word
else:
print "Word not found"
Run Code Online (Sandbox Code Playgroud)
在此先感谢大家!!