Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. text = "Dear NAME, it was nice to meet you on DATE. Hope to talk with you and SPOUSE again soon!"
  2.  
  3. NAME, DATE, SPOUSE
  4. John, October 1, Jane
  5. Jane, September 30, John
  6. ...
  7.  
  8. with open('recipients.csv') as csvfile:
  9. reader = csv.DictReader(csvfile)
  10. for row in reader:
  11. for match in matchedfields:
  12. print inputtext.replace(match, row[match])
  13.  
  14. "Dear John, it was nice to meet you on October 1. Hope to talk with you and Jane again soon!"
  15.  
  16. "Dear Jane, it was nice to meet you on September 30. Hope to talk with you and John again soon!"
  17.  
  18. import copy
  19.  
  20. with open('recipients.csv') as csvfile:
  21. reader = csv.DictReader(csvfile)
  22. for row in reader:
  23. inputtext_copy = copy.copy(inputtext) ## make a new copy of the input_text to be changed.
  24. for match in matchedfields:
  25. inputtext_copy = inputtext_copy.replace(match, row[match]) ## this saves the text with the right
  26. print inputtext ## should be the original w/ generic field names
  27. print inputtext_copy ## should be the new version with all field changed to specific instantiations
  28.  
  29. import string
  30. templateText = string.Template("Dear ${NAME}, it was nice to meet you on ${DATE}. Hope to talk with you and ${SPOUSE} again soon!")
  31.  
  32. import csv
  33. with open('recipients.csv') as csvfile:
  34. reader = csv.DictReader(csvfile)
  35. for row in reader:
  36. print(templateText.safe_substitute(row))
  37.  
  38. with open('recipients.csv') as csvfile:
  39. reader = csv.DictReader(csvfile)
  40. for row in reader:
  41. inputtext = text
  42. for match in matchedfields:
  43. inputtext = inputtext.replace(match, row[match])
  44. print inputtext
  45.  
  46. text = "Dear {0[NAME]}, it was nice to meet you on {0[DATE]}. Hope to talk with you and {0[SPOUSE]} again soon!"
  47.  
  48. with open('recipients.csv') as csvfile:
  49. reader = csv.DictReader(csvfile)
  50. for row in reader:
  51. inputtext = text.format(row)
  52. print inputtext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement