![]() |
Problem with reading and writing to file. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with reading and writing to file. (/thread-19933.html) |
Problem with reading and writing to file. - darktitan - Jul-20-2019 Hi I got a code for a csv database where i can write a bit of data and it saves it to a csv file very simple. My problem is that every time i start the code it overwrite the old csv file and i start up with an empty file. I have no ide what to do. Could any one help me out? #imports import csv #Variables current_ID = 1 new_additions = [] filename = "library.csv" fields = ['ID', 'Title', 'Author', 'Genre', 'Year', 'Location'] data = [] #Code with open(filename, 'w+') as csvfile: reader = csv.DictReader(csvfile) for row in reader: data.append(dict(row)) if len(data) > 0: current_ID = data[:-1].get("ID") current_ID = int(current_ID) + 1 print("------Welcome to the Python Library organiser------") choice = "" while choice.lower() != "x": print("""What would you like to do? 1 - Add a book 2 - Display your Books 3 - Search for a Book""") choice = input(">") if choice == "1": #Code to add a record new_record = { "ID": current_ID, "Title": input("What is the title of the book? >"), "Author": input("What is the author's first name? >"), "Genre": input("What genre is the book? >"), "Year": input("What year was the book released? >"), "Location": input("Where is the book? > ") } new_additions.append(new_record) current_ID = current_ID + 1 print("-"*15) print("New Record added successfully!") elif choice == "2": #Display the data for item in fields: print("%-25s"%item, end='') print("\n") for row in data: for key, val in row.items(): print("%-25s"%val, end='') print("\n") for row in new_additions: for key, val in row.items(): print("%-25s"%val, end='') print("\n") elif choice == "3": #Search the data search_term = input("What would you like to search for?").lower() results = [] for row in data: for key, val in row.items(): if search_term in val.lower(): results.append(row) for item in new_additions: for key, val in item.items(): if search_term in val.lower(): results.append(item) #Display the results if len(results) > 0: for item in fields: print("%-25s"%item, end='') print("\n") for item in results: for key, val in item.items(): print("%-25s"%val, end='') print("\n") else: print("Sorry no records found") elif choice.lower() == "x": print("Thank you! Shutting down.") else: print("Sorry, I didnt recognise that option") if len(new_additions) > 0: with open(filename, 'a') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fields) writer.writeheader() for item in new_additions: writer.writerow(item) RE: Problem with reading and writing to file. - ichabod801 - Jul-20-2019 Opening it in write mode (line 16) is erasing the file. Open it in read mode ('r'). Note that line 21 also has an error. data[:-1] is a slice because of the colon, so it's a list, which has not get method. Change that to data[-1], which will just return the last line read. Why only the first name of the author? RE: Problem with reading and writing to file. - darktitan - Jul-20-2019 (Jul-20-2019, 05:54 PM)ichabod801 Wrote: Opening it in write mode (line 16) is erasing the file. Open it in read mode ('r'). Note that line 21 also has an error. data[:-1] is a slice because of the colon, so it's a list, which has not get method. Change that to data[-1], which will just return the last line read. Thank you. Honesly i dont know why its only the first name. Its not my code i found it on a blog from the raspberry pi home page i tought i could do something else with it. |