在发送到电子邮件的字符串中使用零与一个换行符时,无换行符与双换行符
我正在向电子邮件发送一个字符串,并且我想要每行一个句子,如下所示:
"Loaded LLARY_AR with 0 features
Loaded LLARY_LN with 44 features
Loaded LLARY_PT with 23 features"
但是,当我向字符串连接中添加一个换行符时,我会得到两个换行符而不是一个,如下所示:
"Loaded LLARY_AR with 0 features
Loaded LLARY_LN with 44 features
Loaded LLARY_PT with 23 features"
如果我不包含换行符我得到这个:
"Loaded LLARY_AR with 0 features Loaded LLARY_LN with 44 features Loaded LLARY_PT with 23 features"
这是代码:
msgemail = ""
for fcl in trnlist:
try:
tofc = param["tsde"]+"\\"+param["trema"]+fcl
fromfc = param["msde"]+"\\"+param["mchema"]+fcl
arcpy.DeleteFeatures_management(tofc)
arcpy.Append_management(fromfc, tofc)
msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
del fcl, tofc, fromfc
except:
msgemail +="\nUnsuccessful!! "+fcl
emailto = ["email@to","email@to"]
server = smtplib.SMTP('server.here')
header = "Results:\n"
subject = "Data"
mailmessage = header+msgemail+"\n\nCheck log for details"
message = 'Subject: %s\n\n%s' %(subject, mailmessage)
for e in emailto:
try:
server.sendmail("email@from",e, message)
except:
arcpy.AddMessage(e+" was not sent an email.")
server.quit()
我不明白为什么换行符以这种方式运行..并且是新手..显然这里缺少一些东西。
我发现这可以生成一封格式良好的电子邮件(但不包含 ..GetCount..process 中的必要信息):
msgemail +="\nLoaded"+fcl
虽然这些不会生成格式良好的电子邮件:
msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
msgemail +="\nLoaded "+fromcnt
msgemail +="\nLoaded "+fromcnt+" testing for string at end"
I'm sending a string to an email, and I want one sentence per line like this:
"Loaded LLARY_AR with 0 features
Loaded LLARY_LN with 44 features
Loaded LLARY_PT with 23 features"
But when I add one newline to the string concatenation I get two newlines instead of one, like this:
"Loaded LLARY_AR with 0 features
Loaded LLARY_LN with 44 features
Loaded LLARY_PT with 23 features"
And if I do not include a newline I get this:
"Loaded LLARY_AR with 0 features Loaded LLARY_LN with 44 features Loaded LLARY_PT with 23 features"
Here's the code:
msgemail = ""
for fcl in trnlist:
try:
tofc = param["tsde"]+"\\"+param["trema"]+fcl
fromfc = param["msde"]+"\\"+param["mchema"]+fcl
arcpy.DeleteFeatures_management(tofc)
arcpy.Append_management(fromfc, tofc)
msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
del fcl, tofc, fromfc
except:
msgemail +="\nUnsuccessful!! "+fcl
emailto = ["email@to","email@to"]
server = smtplib.SMTP('server.here')
header = "Results:\n"
subject = "Data"
mailmessage = header+msgemail+"\n\nCheck log for details"
message = 'Subject: %s\n\n%s' %(subject, mailmessage)
for e in emailto:
try:
server.sendmail("email@from",e, message)
except:
arcpy.AddMessage(e+" was not sent an email.")
server.quit()
I don't understand why the newline acts in this manner..and a newbie..obviously missing something here.
I found that this works to produce an email that is nicely formated (but does not include the necessary information from the ..GetCount..process):
msgemail +="\nLoaded"+fcl
While these do not result in a nicely formated email:
msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
msgemail +="\nLoaded "+fromcnt
msgemail +="\nLoaded "+fromcnt+" testing for string at end"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码似乎没有问题,每一行“已加载...”之间应该只有一个换行符。
您用来查看电子邮件的电子邮件客户端可能会将换行符解释为新段落并自动插入该空格。尝试将
\n
替换为
并查看是否会产生您期望的单间距。There does not appear to be an issue with the code, there should only be one newline between each of your 'Loaded...' lines.
It is possible that the email client you are using to view the email is interpreting newlines as new paragraphs and inserting that spacing automatically. Try replacing the
\n
with<br>
and see if that results in the single spacing you expect.我单独使用 smtplib 遇到了同样的问题。我发现更好的方法是这样的:
注意字典的返回!如果某些电子邮件发送失败,smtp.sendmail 将返回失败元组列表。如果所有电子邮件发送失败,则抛出异常,如果全部成功,则字典为空。
如果你检查一下,你就会避免悲伤。
I ran into the same problem using the smtplib by itself. I found that a better way to do it is this:
Note the return of the dictionary! The smtp.sendmail will return it with a list of failure tuples if some emails fail to send. If all emails fail to send, an exception is thrown, and if they all succeed, then the dictionary is empty.
You will save yourself grief if you check it.
'\n' 可能应该出现在行尾。
这听起来很愚蠢,可能行不通,但可能值得一试。
It may be that the '\n' should come at the end of a line.
It sounds dumb and may not work, but it might be worth a shot.