发送格式化的元组列表作为电子邮件正文 [英] Send formatted list of tuples as body of an email
问题描述
也许已经有人问过这个问题,但是我找不到它,我需要帮助.我有一个元组对的列表:
Maybe this has been asked already, but I can't find it, and I need help. I have a list of tuple pairs:
mylist = [('name1', 'name2'), ('name3', 'name4'), ('name5','name6')]
该列表是从MySQL数据库生成的,可以是任意长度.某些名称可能相同.数据类型不一定是元组列表.对我而言,这才是最有意义的.它可以是列表的列表,元组的元组等.字典对我而言没有意义,因为如前所述,任何名称都可以相同.这是在使用django的python中进行的,无法更改.
The list is generated from a MySQL database and can be of any length. It is possible that some of the names are the same. It is not absolutely necessary that the data type be a list of tuples. That's just what made the most sense to me. It can be a list of lists, tuple of tuples, etc. A dictionary doesn't make sense to me, because, as previously stated, any of the names can be the same. This is in python using django, which can't be changed.
无论如何,我想发送一封电子邮件,其中以我的清单"的内容为正文.不过,我想对其进行格式化,以使其看起来像这样的电子邮件正文:
Anyway, I want to send an email with the contents of Mylist as the body. I want to format it, though, so that it looks in the body of the email like this:
name1, name2
name3, name4
name5, name6
换句话说,我希望它看起来像您在电子邮件正文中看到的普通文本.最初,我的想法是我必须使用函数并为输出的每一行生成一个变量.可能有一种方法可以在不为每个换行符创建新变量的情况下完成此任务,但是我无法弄清楚.老实说,我还没有弄清楚.我已经尝试过循环,while循环,递归函数等.没有运气.
In other words, I want it to look like normal text you would see in an email body. Initially, my thinking was that I had to use a function and generate a variable for each line of output. There may be a way to get this done without creating new variables for each newline, but I can't figure it out. To be honest, I haven't figured it out at all. I've tried for loops, while loops, recursive functions, etc. No luck.
这是工作,我真的可以使用一些帮助.谢谢
This is for work, and I could really use some help. Thanks
推荐答案
想到的最Python化的方法是:
The most Pythonic way that comes to mind would be:
body = '\n'.join('%s, %s' % pair for pair in mylist)
这篇关于发送格式化的元组列表作为电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!