I'm trying to make it so you can edit a single client's details in a csv file, and while the code I wrote runs it for some reason adds a gap in between each client as well as changing the client - i'd really appreciate if someone could tell me why this is happening.
Here's an excerpt of my csv:
first_name,last_name,title,pronouns,dob,occupation,account_balance,overdraft_limit
Genovera,Willgoss,Mrs,Female,25/05/2022,Graphic Designer,2315.16,46.48
Garner,Coupman,Ms,Male,14/04/2022,General Manager,2200.76,2.28
Jens,Eldrid,Honorable,Male,13/11/2021,Research Associate,967.64,79.15
The code i'm running:
if choice == "4":
editClient = int(input("Please enter the index number of the client you wish to edit:"))
print("Please enter the details for each of the following:")
for i in range(len(existing_clients[0])):
newDetails = input("Enter new data for " str(existing_clients[0][i]) ":")
existing_clients[editClient][i] = newDetails
changes = input("Are you sure you'd like to make these changes? Enter Yes or No")
if changes == ("Yes"):
with open("mock_data.csv", "w ") as file:
reader = csv.writer(file)
for i in range(len(existing_clients)):
reader.writerow(existing_clients[i])
And what my csv looks like after i've changed a client: (I just changed all of Jens details to 1)
first_name,last_name,title,pronouns,dob,occupation,account_balance,overdraft_limit
Genovera,Willgoss,Mrs,Female,25/05/2022,Graphic Designer,2315.16,46.48
Garner,Coupman,Ms,Male,14/04/2022,General Manager,2200.76,2.28
1,1,1,1,1,1,1,1
I haven't tried anything because i've got no idea whats making this happen - I've only been programming a month and am very lost.
CodePudding user response:
I believe it is adding an extra carriage return when writing. Try changing this line:
with open("mock_data.csv", "w ") as file:
to
with open("mock_data.csv", newline= "", "w ") as file: