To start with, all your recipients are always travelling from Manchester; this is because your places list is looped through in it's entirety for each recipient name, and the document is saved AFTER the loop.
Recipient names are being switched from the place name, but when you are assigning the address in your second for loop, you are using the original letter template instead of the template modified when applying the name, the assignment of new_letter
is replaced with the original letter_contents
.
This would be resolved by swapping:
new_letter = letter_contents.replace(ADDRESS, stripped_place)
with
new_letter = new_letter.replace(ADDRESS, stripped_place)
Your idea of using dictionaries instead of lists is a good one. After dealing with countless mailmerge projects, one of the most difficult and necessary problems is ensuring all client details are always kept in sync. I would assign each recipient a unique ID to tie data in to. Reading both data fields into a dictionary under the same key will make it easier when you start dealing with big datasets and large numbers of fields.
An example of this, using defaultDict:
from collections import defaultdict
PLACEHOLDER = "[name]"
ADDRESS = "[place]"
fieldMapper = defaultdict(dict)
with open("invited_names.txt") as names_files:
names = names_files.readlines()
with open("invited_places.txt") as places_files:
places = places_files.readlines()
with open("starting_letter.txt") as letter_file:
letter_contents = letter_file.read()
docID = 0 # create a document ID
for name in names:
stripped_name = name.strip()
fieldMapper[docID]['name']=stripped_name
docID +=1
docID = 0
for place in places:
stripped_place = place.strip()
fieldMapper[docID]['address']=stripped_place
docID +=1
for document,fields in fieldMapper.items(): # run through each recipient / Doc ID in dictionary
new_letter = letter_contents.replace(ADDRESS, fields.get('address'))
new_letter = new_letter.replace(PLACEHOLDER, fields.get('name'))
with open(f"letter_for_{fields.get('name')}.txt",mode="w") as completed_letter:
completed_letter.write(new_letter)
If at all possible, I would add resiliency to your source data to make it difficult to mix up different fields of recipient information. As it is, if a blank line was entered halfway through your data, any further data would be out of sync. This could be accomplished by either having one source data file with comma separated values (or any other delimiter), or using a delimited recipient ID in both text files to ensure customer records will always be kept in sync.
Reading CSV files in Python
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…