1

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!

1
  • please provide sample input and desired output Commented Jan 25, 2017 at 20:34

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the reply, what's the significance of 1/25/2015 in your example?
updated that value, its mean to hold the date of the code run
I tired running your example and it's printing out dates that are more than 3 days away?
can you add few inputs and outputs to the question?
maybe my code example was off. I need this to print out the column account name if the column expiration date happens to be 3 days away.
|
0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.