gpt4 book ai didi

python - 为什么这个程序从 Aptana IDE 运行而不是在 python 命令中运行?

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:23 25 4
gpt4 key购买 nike

以下代码会产生

htmlbody,emailaddress = ConnEmailParser()
TypeError: 'NoneType' object is not iterable

错误,但在 IDE 中工作正常。正在发送电子邮件并生成 PDF 文件。但在命令行中,只有 ConnEmailParser() 可以正常工作。

我该怎么做才能使程序顺利运行?

这里是代码片段:

import imaplib
import email
import quopri
from cStringIO import StringIO
import ho.pisa as pisa
from datetime import datetime
import logging
import smtplib
from email.generator import Generator
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email import Encoders

def sendPdfSurvey():

def ConnEmailParser():
try:
saver = StringIO()
SERVER = "server"
USER = "user"
PASSWORD = "nono"
subject="a certain phrase"
imap4 = imaplib.IMAP4(SERVER)
imap4.login(USER, PASSWORD)
imap4.select()
imap4.search(None, 'ALL')
typ, data = imap4.search(None,'(UNSEEN SUBJECT "%s")' % subject) #looking for phrase
count = 0
for num in data[0].split():
count = count + 1 # this is stupid but it gets just oone messsage
if count ==1 :
typ, data = imap4.fetch(num,'(RFC822)')
email7 = email.message_from_string(data[0][1])
varSubject = email7['subject']
# The phase from above has an emailaddress in the subject field
emailaddressraw = varSubject[13:]
emailaddressmedium = emailaddressraw.replace("<","")
emailaddress= emailaddressmedium.replace(">","")
msg = email.message_from_string(data[0][1])
typ, data = imap4.store(num,'+FLAGS','\Seen')
a=str(msg)
i= a.decode('quopri').decode('utf-8')
saver.write(i)
savercontent = saver.getvalue()
# body of the email is html and all of the header elements get deleted
planhtml = savercontent.split('<html xmlns="http://www.w3.org/1999/xhtml">')[1].strip()
return planhtml, emailaddress.strip()
saver.close()
imap4.close()
imap4.logout()
except:
"could not get participant email!"

#here somewhere is the trouble maker

htmlbody,emailaddress = ConnEmailParser()
filenamePDF= "UmfrageEthics_%s.pdf" % datetime.utcnow().strftime('%m%d%H%M%S%f')

def printPdf():
try:
html = htmlbody
result = StringIO()
pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), result, encoding='UTF-8')
filetoparse=open(filenamePDF,"wb")
filetoparse.write(result.getvalue())
filetoparse.close()
#pisa.startViewer(filenamePDF)
except:
print "could not write PDF"


def sendPdfParticipant():
try:
oNachricht = MIMEMultipart()
oNachricht['From'] = 'some@email.com'
oNachricht['To'] = emailaddress
oNachricht['Date'] = formatdate(localtime= True)
oNachricht['Subject'] = 'A subject'
oAnhang = MIMEBase('application','octet-stream')
oAnhang.set_payload(open(filenamePDF,'rb').read())
Encoders.encode_base64(oAnhang)
oAnhang.add_header('Content-Disposition', 'attachment;filename = some.pdf')
message_text_plain = u'Some message...'
oNachricht.attach(MIMEText(message_text_plain.encode('utf-8'), 'plain', 'UTF-8'))
io = StringIO()
g = Generator(io, False)
g.flatten(oNachricht)


oNachricht.attach(oAnhang)


user = 'user'
pwd = 'pwd'
smtpserver = smtplib.SMTP("smtp.raumopol.de",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(user, pwd)
smtpserver.sendmail('som@eemail.com',emailaddress,oNachricht.as_string())
smtpserver.quit()
except:
print"could no send data"

sendPdfSurvey()

最佳答案

def ConnEmailParser() ,检查是否 dataNone在调用data[0].split()之前。如果是None ,检查是否 imap4已正确启动并且登录是否有效。如果是这种情况,请发布代码的缩减部分,指出实际出错的地方。

正如 @Jim Garrison 指出的那样,回溯会对我们(和您)有很大帮助。方法如下:

import traceback

try:
some_code...
except TypeError:
traceback.print_exc()
<小时/>

编辑(根据您的评论)

对 unicode 字符串进行编码时,您会将不属于 ASCII 的字符替换为代码“\xfc”。显然, saver.write() 在首先将其解释为 unicode 之后尝试对已经编码的文本进行编码(您得到 u'\xfc' )。所以不要对自己进行编码。如果仍然出错,请查看如何更改编码(例如“utf-8”、“cp1252”)。使用

获取操作系统的当前编码
import locale
encoding=locale.getlocale()[1]

关于python - 为什么这个程序从 Aptana IDE 运行而不是在 python 命令中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7604399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com