I am struggling with the csv module. I have a sample CSV file that has 5000 lines (each line contains 7 values 0 or 1) with headers. I want to iterate through file in read mode and append file in write mode with new column values (prediction), but iteration stop after 478th row (like in sample code):
import csv
import random
def input_to_csv():
prediction = [round(random.uniform(0, 1), 0) for _ in range(1, 5000)]
combined_set = list(map(str, prediction))
export_columns = ['COLUMN ' + str(n) for n in range(1, 8)] + ['OUTPUT']
rr = 0
with open('test.csv', 'r') as input_file:
csv_input = csv.reader(input_file)
next(csv_input)
with open('test.csv', 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(export_columns)
for row in csv_input:
rr += 1
print(rr)
I have checked length of the csv_input file using row_count = sum(1 for _ in input_file) which gave me 5000 lines.