0

Problem:

Looking for assistance in adding user input such as Last Name, First Name, Credit Card number, etc etc to a csv file in Python. I assume I would have to add similar code for each of the user inputs (lastName, firstName, arrivalDate, departureDate, enterRate, enterCC, enterEXP)

Context:

This program mimics a hotel property management system, where guests information such as room number, arrival, departure, CC# are stored. In addition, the program allows the user to input new guests with the 'Make Reservation' menu selection.

Relevant code is attached - thank you!

elif menuSelect == "6":
    lastName = input("Please Enter a Last Name: ")
    firstName = input("Please Enter a First Name: ")
    arrivalDate = input("Please Enter an Arrival Date: ")
    departureDate = input("Please Enter a Departure Date: ")
    enterRate = input("Please enter Rate Amt: $")
    enterCC = input("Please enter a valid Credit Card: ")
    enterEXP = input("What is the Expiration Date? mm/yy: ")
  • 1
    You could look at the csv module – steviestickman Jun 11 '20 at 16:58
  • are you trying to create the new csv file from user input or adding the input to existing csv file – deadshot Jun 11 '20 at 16:59
  • adding the input to an existing csv file - thanks! – mattthew_james Jun 11 '20 at 16:59
  • Does this answer your question? [append new row to old csv file python](https://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python) – deadshot Jun 11 '20 at 17:01
  • 1
    Are you looking to create CSVs for every guest? If it can contain info of multiple guests then,You can create a dataframe with those details as columns and append the inputs everytime you get a new guest. Pandas [to_csv](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html) lets you download these data frames as csvs – Raghul Raj Jun 11 '20 at 17:01

1 Answers1

1

You could use the python built-in csv library:

To use it simply write:

import csv
with open('filename.csv', 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile)

and to write a row:

    csvwriter.writerow(['Column1', 'Column2', 'Column3'])

Note: If you want to add input to an existing file replace the 'w' in open with 'a'

Amin Guermazi
  • 1,632
  • 9
  • 19