如何使用不同的打印语句创建不同的 txt 文件?
How do I create different txt files with different print statements?
我在这个代码分配上遇到了很多麻烦。我目前正在尝试做的是为婚礼请柬创建单独的文件。我想做的是为每个新打印的 statement/invitation 创建一个新的 txt 文件。以下是我一直在尝试做的事情,我目前无法将每个打印语句放入一个新文件名的新 txt 文件中。
import os
if os.path.isfile("guest_list.txt"):
# Open files for input
infile = open("guest_list.txt", "r")
line = infile.readline() # Read a line
while line != '': # read until end-of-file
e = open("event_details.txt","r")
splitLine = line.split()
letter = print("Dear,", splitLine[0],"",splitLine[1],"\n" \
"With great pleasure we are delighted to invite you to our wedding.\n" \
"We would be honored if you and your family could save the date.\n",e.read())
w = open("wedding_invite.txt","w")
w.write(''.join("Dear",str(splitLine[0]),"",str(splitLine[1])))
w.write("With great pleasure we are delighted to invite you to our wedding.")
w.write(''.join("We would be honored if you and your family could save the date.\n",e.read()))
#read next line
line = infile.readline() # Read next line from file
e.close()
w.close()
infile.close()
您将在每次迭代中覆盖文件 wedding_invite.txt
。
您想要为每个来宾创建一个新的 .txt 文件:
import os
if os.path.isfile("guest_list.txt"):
#read all the guest names into a list
with open("guest_list.txt", "r") as infile:
#read all the guest names to a list
guest_list = infile.read().strip().split("\n")
#read the event_details file just once instead of in each loop
with open("event_details.txt") as infile:
date = infile.read().strip()
#loop through the guest_list and create custom invitations
for line in guest_list:
words = line.split(" ")
#create new file for each guest
with open(f"{line}.txt", "w") as outfile:
outfile.write(f"Dear {words[0]} {words[1]},\n")
outfile.write("With great pleasure we are delighted to invite you to our wedding.\n")
outfile.write(f"We would be honored if you and your family could save the date.\n{date}")
我在这个代码分配上遇到了很多麻烦。我目前正在尝试做的是为婚礼请柬创建单独的文件。我想做的是为每个新打印的 statement/invitation 创建一个新的 txt 文件。以下是我一直在尝试做的事情,我目前无法将每个打印语句放入一个新文件名的新 txt 文件中。
import os
if os.path.isfile("guest_list.txt"):
# Open files for input
infile = open("guest_list.txt", "r")
line = infile.readline() # Read a line
while line != '': # read until end-of-file
e = open("event_details.txt","r")
splitLine = line.split()
letter = print("Dear,", splitLine[0],"",splitLine[1],"\n" \
"With great pleasure we are delighted to invite you to our wedding.\n" \
"We would be honored if you and your family could save the date.\n",e.read())
w = open("wedding_invite.txt","w")
w.write(''.join("Dear",str(splitLine[0]),"",str(splitLine[1])))
w.write("With great pleasure we are delighted to invite you to our wedding.")
w.write(''.join("We would be honored if you and your family could save the date.\n",e.read()))
#read next line
line = infile.readline() # Read next line from file
e.close()
w.close()
infile.close()
您将在每次迭代中覆盖文件 wedding_invite.txt
。
您想要为每个来宾创建一个新的 .txt 文件:
import os
if os.path.isfile("guest_list.txt"):
#read all the guest names into a list
with open("guest_list.txt", "r") as infile:
#read all the guest names to a list
guest_list = infile.read().strip().split("\n")
#read the event_details file just once instead of in each loop
with open("event_details.txt") as infile:
date = infile.read().strip()
#loop through the guest_list and create custom invitations
for line in guest_list:
words = line.split(" ")
#create new file for each guest
with open(f"{line}.txt", "w") as outfile:
outfile.write(f"Dear {words[0]} {words[1]},\n")
outfile.write("With great pleasure we are delighted to invite you to our wedding.\n")
outfile.write(f"We would be honored if you and your family could save the date.\n{date}")