I am new to python, I am trying to input some missing data into a csv file. I also want to divide the first values by 2.
This is the Input file:
5,1
6,1
10,1
Output file, fill in missing numbers from 0 and divide the first values by 2.
0/2, 0
1/2, 0
2/2, 0
3/2, 0
4/2, 0
5/2, 1
6/2, 1
7/2, 0
8/2, 0
9/2, 0
10/2, 1
This is my code so far, I started by opening the initial file then write the missing values into the new file starting from 0.
import csv
with open(r"C:\Users\Desktop\initial.csv") as f:
reader = csv.reader(f)
with open("C:\\Users\\Desktop\\newfile.csv", "w") as csvfile:
csvwriter = csv.writer(csvfile)
for i in range(11):
csvwriter.writerow([i,0])
I am stuck on how to check the initial file, compare to see if the new value is there, if it’s there, copy over to new file. Any help will be appreciated. I am also opened to an easier way of doing this. Thanks.