I have spent the past two days on this and I've drawn blank, hopefully someone can help.
I have 3 values assigned to variables that are always going to be the same. I then have a fourth value assigned to a variable that is going to be different each time on user input. The intention is to take user input for all 4 values. On the fourth value I want the program to ask me for input repeatedly until I press "q" for example. Each time I enter a value it appends to a CSV file with the existing 3 values already entered. After q, continue with the script.
value1 = input("enter value1 ")
value2 = input("enter value2 ")
value3 = input("enter value3 ")
value4 = input("enter value4 ")
value4 = input("enter value4 ")
...
...
...
q
CSV file should be like this, ensuring that the first three values are constant but the fourth value is different per each entry.
column1,column2,column3,column4
value1,value2,value3,something_new
value1,value2,value3,something_new2
value1,value2,value3,something_new3
value1,value2,value3,something_new4
value1,value2,value3,something_new5
value1,value2,value3,something_new6
...
...
...
I had the below code that takes the values and adds 1 row in a CSV file each time. I've tried using file mode 'a' but I can't seem to add a new row in the CSV for the next. And when I add a for loop to the code below I get the "TypeError: 'DictWriter' object is not iterable" error.
with open('file_append.csv', 'w', newline='') as file:
fieldnames = ['column1', 'column2', 'column3', 'column4']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'column1': value1, 'column2': value2, 'column3': value3, 'column4': value4})
I'm getting lost on adding for/while loops, if statements, if anyone can't point me in the right direction or show me a working solution it would be much appreciated
I have looked at many stack overflows and blogs but nothing is helping me solve this.
how to append to a new row when writing to CSV file