1

I'm making a movie theatre booking system as part of a school project so my knowledge about python is fairly limited.

I have the following code to import the csv file, which stores the status of the seats (0 = not booked and 1 = booked)

import csv

with open('seats.csv', newline='') as csvfile:

    seats = list(csv.reader(csvfile))

Here's my code for booking the seat:

def bookSeat():
  print("Booking a Seat by Row/Column")
  booked = False
  while booked == False:
    row = int(input("Enter a row number (between 0 and 5)"))
    column = int(input("Enter a column number (between 0 and 7)"))
 
    if seats[row][column]==1:
      print("This seat is already booked.")
    else:
      print("This seat is empty.")
      print("Booking seat...")
      seats[row][column]=1
      print("We have now booked this seat for you.")
      booked=True

So my question is after booking a seat (replacing the 0 with a 1), how do I update the csv file to reflect this change?

Thanks for all your help in advance!!

5
  • Try using csv.writer? Commented Oct 16, 2020 at 15:34
  • @OneCricketeer does this overwrite the previously written data or create a new csv file? Commented Oct 16, 2020 at 15:41
  • Depends what you pass into open() function Commented Oct 16, 2020 at 15:47
  • You can also write to a CSV file using a writer object and the .write_row() method Commented Oct 16, 2020 at 15:48
  • @OneCricketeer So what would I need to do if I want to overwrite the original csv file? Commented Oct 16, 2020 at 15:49

2 Answers 2

2

This can be acheived using csv.writer() object which has the writerows property.

def bookSeat():

    print("Booking a Seat by Row/Column")
    booked = False
    while booked == False:

        row = int(input("Enter a row number (between 0 and 5)"))
        column = int(input("Enter a column number (between 0 and 7)"))

        if seats[row][column]==1:
            print("This seat is already booked.")
        else:
            print("This seat is empty.")
        print("Booking seat...")
        seats[row][column]=1
        print("We have now booked this seat for you.")
        booked=True

    # Rewrite the CSV with the new list of seats
    writer = csv.writer(open('seats.csv', 'w'))
    writer.writerows(seats)
Sign up to request clarification or add additional context in comments.

Comments

0

Hello

If you want you can also do this with pandas df: (it will simply overwirite it every time)
import pandas as pd

seats = pd.read_csv('seats.csv', sep=',')

def bookSeat():
    print("Booking a Seat by Row/Column")
    booked = False
    while booked == False:
        row = 'row ' + input("Enter a row number (between 0 and 5)")
        column = int(input("Enter a column number (between 0 and 7)"))

      if seats[row][column]==1:
          print("This seat is already booked.")
      else:
          print("This seat is empty.")
          print("Booking seat...")
          seats[row][column]=1
          print("We have now booked this seat for you.")
          booked=True
bookSeat()

seats.to_csv('seats.csv', sep=',')`

note: i put a 'row ' + input in the row variable, so your csv should look like this:

row 0,row 1,row 2,row 3,row 4,row 5

0,0,0,0,0,0

0,0,0,0,0,0

0,0,0,0,0,0

0,0,0,0,0,0

0,0,0,0,0,0

0,0,0,0,0,0

0,0,0,0,0,0

0,0,0,0,0,0

this will create a better overview over the occupied rows!

Good luck with your code!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.