45. Writing Delimited Files

  • Writing delimited files is easy. You simply combine the items for each line, separated by a comma, with a \n on the end and then write it to the file
#Open file for append (add to)
file = open('addresses.csv','a')

name = "Jay Jones"
address = "56 3rd St"
city = "Delaware"
phone = "08556 474562"

#Write a line separated by commas, \n on the end
file.write(name + "," + address + "," + city + "," + phone + "\n")

#Close file
file.close()

Tasks

  1. Type in the code above and make sure it works
  2. Modify the program to accept user input
  3. Save and label your code

Extension Task

You can implement a for or while loop in your program that will allow you to enter details for multiple people, either until a specific character is pressed or a set number of details have been entered. Can you make this work?