2

I'm currently using pretty table for a Barbershop P.O.S implementation where the program needs to generate reports that an admin user can view. The reports are copied line by line from a csv(converted into a pretty table) each time a user logs that a service from the csv has been performed.

I'd like these lines to be written into a new report csv file, and be accessed by the admin through both the python interpreter and other file systems.

So far, the program copies the lines from the original csv (it's printing them out) but isn't writing them into the report log csv.

Does anyone know how to implement this?

Here's my code: Module that opens csv with Services and converts it into a table:

from prettytable import from_csv

def servicetable():
    fp = open("Barbershop.csv", "r")
    file = from_csv(fp)
    fp.close()
    print(file)

Function that copies the line and pastes it into log.csv

def log_service():
    while True:
      try:
        response3 = input("Please choose service from list using the ServiceID.\n")
        response3 = int(response3)

        #copy service table into another table
        fp = open("Barbershop.csv", "r")
        file_copy = from_csv(fp)   #filecopy is a variable that holds the table
        fp.close()
        x = file_copy[response3:(response3+1)]  #copy this row of the table
        print(x)                                #print it out as confirmation

        y = open("log.csv", 'wb') #create and open another csv file
        writer = csv.writer(y, delimiter='  ',quotechar='"',quoting= csv.QUOTE_NONE) #create writer object
        writer.writerows(y) #write each iteration value of x into log.csv

        choice2 = input("Continue yes/ no:").lower()
        if not(choice2=="yes" or choice2=="y"):
         break
      except ValueError:
        print("Please enter valid ID")

Of course, a simpler or more efficient implementation of the concept is also welcome. As always, many thanks!

mabishi
  • 115
  • 1
  • 1
  • 8
  • Have a look at the y (writer) and consider using the x (data) instead `writer.writerows(y) #write each iteration value of x into log.csv` – Martin Swanepoel Apr 06 '16 at 13:39
  • @Martin, do you mean? writer.writerows(x) I get this error TypeError: object of type 'PrettyTable' has no len() – mabishi Apr 06 '16 at 14:59
  • try `writer.writerow(...)` instead. You're using a row in your instance and not an iterator. – Martin Swanepoel Apr 06 '16 at 15:03
  • I still encounter the same error with writer.writerow(x) TypeError: object of type 'PrettyTable' has no len() Could it be that pretty tables cannot be converted back into csv format? – mabishi Apr 06 '16 at 15:50

0 Answers0