我有脚本,它收集一些数据:
...
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
答案 0 :(得分:3)
使用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))