【问题标题】:Format list in email电子邮件中的格式列表
【发布时间】:2015-01-18 09:29:35
【问题描述】:

我有脚本,它收集一些数据:

...
REPORT = []
...
if re.search('^-', line):
    logging.info('File %s in line %d: %s' % (file, num, line))
    REPORT.append('File %s in line %d: %s' % (file, num, line))
...

然后发送邮件:

body = email.mime.Text.MIMEText("""

Report:

%s

""" % REPORT)

msg.attach(body)

但它以“原始”视图出现,例如:

报告:

['File services.PROD.properties in line 29: - \r\n', 'File services.PROD.properties in line 30: - Url=https://someurl.com/reporting\r\n', 'File services.properties in line 44: - gUrl=http://someurl.com:11704/uu.dll\r\n', ']

如何格式化它以发送通常的行/字符串?

Python 2.6

【问题讨论】:

  • 在顶部的for-loop 之后,REPORT 是什么样的?你能发布那个,或者,或者,line的内容吗?
  • @jkalden 已编辑报告示例输出
  • 好的,经过测试。我接受@falsetru 给出的答案

标签: python string list python-2.6


【解决方案1】:

使用str.join 将字符串与换行符连接起来:

>>> '\n'.join(['line1', 'line2', 'line3'])
'line1\nline2\nline3'
>>> print('\n'.join(['line1', 'line2', 'line3']))
line1
line2
line3

body = email.mime.Text.MIMEText("""

Report:

%s

""" % '\n'.join(REPORT))

【讨论】:

  • @setevoy,您编辑的列表包含回车和换行;如果你不想要额外的换行符,你可以使用''.join(REPORT)
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 2014-07-26
  • 2020-05-07
  • 2020-04-20
  • 2017-04-18
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
相关资源
最近更新 更多