I'm making a data viewer program and for part of it I need to make an import function that allow users to import a CSV file.
The CSV file contains sales amount, year, month and day.
I am also using a text file to keep track of the imported file so the files cannot be imported more than once.
I'm just lost on where to even start that.
import salesimport csvwith open('all_sales.csv', newline='') as f: data = list(csv.reader(f))#display a titledef display_welcome_message(): print("Welcome to data viewer program")#display a titledef display_welcome_message(): print("Welcome to data viewer program")def display_menu(): print("enter sales data") print() print("COMMAND MENU") print("View - View All Sales") print("add - Add sales") print("Menu - Show menu") print("Import - Import file") print("Exit - Exit Program") print()total_amount=0FILENAME = "all_sales.csv"def read_sales(): all_sales = [] with open(FILENAME, newline="") as file: reader = csv.reader(file) for row in reader: all_sales.append(row) return all_salesdef write_sales(all_sales): with open(FILENAME, "w", newline="") as file: writer = csv.writer(file) writer.writerows(all_sales)#def create_file(all_sales): #with open(FILENAME, "w") as file: #file.write()def add(all_sales): amount1 = sales.get_amount() year1 = sales.get_year() month1 = sales.get_month() day1 = sales.get_day() #month1, day1, year1, print(" "), "amount:", amount1 add_sales = [] add_sales.append(month1) add_sales.append(day1)a add_sales.append(year1) add_sales.append(amount1) all_sales.append(add_sales) write_sales(all_sales) print(str(month1), " - ", day1, " - ", year1, "was added")def importfile(all_sales): print("Enter a file name to import: ")def view(all_sales): i = 0 total = 0 if len(all_sales) == 0: print("there are no sales data in the list. \n") return else: print(f" Date Quarter Amount") print("---------------------------------------------------------") while i < len(all_sales): print(str(i+1),". ",all_sales[i][2],"-",all_sales[i][0],"-",all_sales[i][1]," "," $",all_sales[i][3]) total += all_sales[i][3] i += 1 print("---------------------------------------------------------") print("TOTAL: $", total)def calculate_quarter(): if month1 <= 3: quarter1 = 1 elif month1 > 3 and month1 <= 6: quarter1 = 2 elif month1 > 6 and month1 <=9: quarter1 = 3 elif month1 > 9 and month1 <=12: quarter1 = 4def main(): display_welcome_message() display_menu() all_sales = [] sales_list = [] while True: command = input('enter command: ') if command == 'view': view(sales_list) elif command == 'add': add(sales_list) elif command == 'menu': display_menu() elif command == 'exit': break else: print("not a valid command. Please try again. \n") print("Bye!")if __name__ == "__main__": main()
def importfile(all_sales): filename = input("Import file name: ")
I have what I'm trying to work with in the def importfile(all_sales): but I'm not sure how to get it to look like what I need it to, see this reference: