我正在尝试编写一个程序,将相同的字母写入每个收件人和每个地址,但每个字母都有不同的名称和地址。
这是我到目前为止所做的。
def main():
recipients = [] #should be names
adresses = [] #letters and numbers
filename = input("Please enter the name of the template file: ")
file = open(filename, 'w')
recipients = input("Please enter names of recipients: ")
adresses = input("Please list addresses: ")
for names in recipients:
letter = "New York, New York\n17 November 2013\n \n", recipients[names], "\n" ,adresses, "\n\nDear",recipients[names],"\n\n" \
"Thank you for your service to New York City, and\nin particular, to the education of its residents.\nThose in", adresses, "appreciate it!"\
"\n\nBest wishes to", recipients[names], "and your family,\n\n"
file.write(str(letter))
file.close()
main()
当它写出文件时,它只写一个字母并将所有内容放在一起。
我想要做的是写一封信给不同的人和SAME文件中的不同地址。
实施例。这将全部在SAME文件中。
New York, New York
11 March 2013
person1
Bronx NY
Dear person1,
Thank you for your service to New York City, and,
in particular, to the education of its residents.
Those in Bronx NY appreciate it!
Best wishes to person1 and your family
New York, New York
11 March 2013
Person2
New York NY
Dear person2,
Thank you for your service to New York City, and,
in particular, to the education of its residents.
Those in New York NY appreciate it!
Best wishes to person2 and your family
任何帮助?
使用Python 3.x
提前致谢
答案 0 :(得分:2)
您的代码存在一些问题。
首先,您要求提供名称和地址列表,但只能从input
获得一个字符串。如果您希望用户指定多个值,则需要以某种方式解析该输入字符串以拆分值。例如,你可以用逗号分隔:
recipients_str = input("Please enter names of recipients: ")
recipients = recipients_str.split(",")
你的下一个问题是你的循环没有达到预期的效果。在Python中,for
循环为您提供循环序列中的值,而不是索引。所以,试试:
for name in recipients:
或者,由于您还有一个与名称列表对应的地址列表,请使用zip
并同时获取名称和地址:
for name, address in zip(recipients, addresses):
接下来,您在创建字母字符串时遇到问题。你现在正在制作一个字符串元组,而不是单个元组。更好的方法是使用str.format
来处理将各种值插入模板:
letter_template = """New York, New York
11 March 2013
{name}
{address}
Dear {name},
Thank you for your service to New York City, and,
in particular, to the education of its residents.
Those in {address} appreciate it!
Best wishes to {name} and your family
"""
letter = letter_template.format(name=name, address=address)
答案 1 :(得分:0)
您需要为每个收件人写一个单独的文件,而不是写入您当前正在执行的同一文件:
def main():
recipients = [] #should be names
adresses = [] #letters and numbers
recipients = input("Please enter names of recipients: ")
adresses = input("Please list addresses: ")
for names in recipients:
letter = "CONTENTS OF LETTER"
filename = input("Please enter the name of the template file: ")
file = open(filename, 'w')
file.write(str(letter))
file.close()
main()
请注意,每次循环迭代都会提示您输入新的模板文件名。您还需要确保每个模板名称都是唯一的,以便不会覆盖先前的文件。
答案 2 :(得分:0)
你的问题是你需要为每个字母写一个唯一的文件,下面的代码部分应该修改为每个字母创建一个文件,你可以做一些事情,比如创建一个计数器并将其附加到文件名以保持他们独一无二。
for names in recipients:
letter = "New York, New York\n17 November 2013\n \n", recipients[names], "\n" ,adresses, "\n\nDear",recipients[names],"\n\n" \
"Thank you for your service to New York City, and\nin particular, to the education of its residents.\nThose in", adresses, "appreciate it!"\
"\n\nBest wishes to", recipients[names], "and your family,\n\n"
file.write(str(letter))
file.close()