I am writing a program that opens a csv file which contains sales data and deletes records if they are from a certain day - for some reason this only works for approximately half of the records in question, instead of deleting all data from e.g. 03.12.2013 and 04.12.2013, it deletes about half of the records from the 3rd and half of the records from the 4th. In the original file, all of the dates are in exactly the same format so i i can't work out why it's treating some of them differently.
Here is the code:
import csv
from datetime import date, timedelta, datetime
with open('C:\\Phocas-Automation\\fullfiletest.csv', 'rb') as f:
reader = csv.reader(f)
counter = 0
existing_transactions = []
columns = []
for row in reader:
if counter == 0:
for col in row:
columns.append(col)
else:
transaction = {}
for i, v in enumerate(row):
transaction[columns[i]] = v
existing_transactions.append(transaction)
counter += 1
days=[]
for x in transactions: # transactions is a similar csv which contains data from 2 specific days
if str(x['date']) not in days:
days.append(str(x['date']))
print days
FMT = '%d/%m/%Y'
counter=0
for x in existing_transactions:
t2 = datetime.strptime(x['date'], FMT)
for z in days:
if str(t2)==str(z):
existing_transactions.remove(x)
ofile = open('C:\\Phocas-Automation\\Output\\Phocasfile.csv', "wb")
writer = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
for x in headersdict:
writer = csv.DictWriter(ofile, fieldnames=columns, dialect='excel')
writer.writerows([x])
for x in existing_transactions:
writer = csv.DictWriter(ofile, fieldnames=columns, dialect='excel')
writer.writerows([x])
thanks in advance for any help.