Reputation: 21
I have a python program that will print out the csv columns “expiration date” and “account name”. How would I write the code to only print out the account columns if the date in “expiration date” is only 3 days away from the time the code is executed?
Example of csv
expiration date account name 1/2/2017 mary 4433 12/25/2018 bob 1244 1/31/2017 david 1234
code so far
import csv
with open('ResellerAccounts-1.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
accounts = []
expiries = []
for row in readCSV:
account = row[0]
expire = row[7]
expiries.append(expire)
accounts.append(account)
print(accounts)
print(expiries)
thanks!
Upvotes: 1
Views: 1286
Reputation: 21
Got it working with this and the numbers to write out to txt file. Thanks everyone for the help!
import datetime
import csv
from datetime import datetime,timedelta
#date, replace with the number gap you need
days=3
dt=(datetime.now() + timedelta(days)).strftime('%#m/%d/%Y')
text=open("call.txt","w")
with open("ResellerAccounts-1.csv", 'r') as f:
reader = csv.reader(f)
next(reader)
for r in reader:
if r[7]==dt:
#print the row mathing the date
#print (r)
t=r[0].split(" ")
number=int(t[1])
#print the number and the incremented number
#print (t[1],number)
text.write(str(number))
text.write("\n")
print("Done..!!")
text.close()
Upvotes: 0
Reputation: 9711
You can add 3 days to the current day and then compare that value against expiry date in row[0] if they match the print the account
import datetime
import csv
i = datetime.datetime.now()
end_date = i + datetime.timedelta(days=3)
end_date=end_date.strftime("%m/%d/%Y")
with open('ResellerAccounts-1.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
next(readCSV, None) # skip the headers
for row in readCSV:
#compare the expiration date from row against current date + 3
if datetime.datetime.strptime(row[0],"%m/%d/%Y")==datetime.datetime.strptime(end_date,"%m/%d/%Y"):
print row[2] # should print all rows matching the date 1/28/2017 for today's run
sample input
1/28/2017, mary, 4433
12/25/2018, bob, 1244
1/31/2017, david, 1234
output
4433
Upvotes: 1