I have a simple Python script that writes a sub(CSV)file after reading a large CSV file.
import csv
import os
large_file = "D:\Total_data.csv"
with open(large_file, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='|')
path = "D:/CSV_Files/"
if not os.path.exists(path):
os.makedirs(path)
writer1 = csv.writer(open(path+"A1_subfile"+".csv", 'wb'), delimiter = '|')
#writing data to the CSV file only if first column has value 'A1'
for row in reader:
if (row[0]=='A1'):
writer1.writerow(row)
csvfile.close()
The problem I am facing is that when I run the script for first time, some rows don't come in A1_subfile.csv file(say only 96 or 97 rows are shown out of 100 rows).
When I run the exact SAME script for the second time, then all rows are shown in the subfile(now all 100 rows can be seen).
Even if I wait for some minutes after executing for the first time, I don't get complete output in new CSV file. And if I simply execute code 2 times, I can see complete output immediately.
I have searched, but couldn't get over this issue. Please help to overcome this.