2017-01-25 6 views
1

私はcsvの列 "有効期限"と "アカウント名"を印刷するpythonプログラムを持っています。 「有効期限」の日付がコードが実行されてからわずか3日である場合、アカウント列を印刷するだけのコードを書くにはどうすればよいですか?Pythonは日付に基づいてCSVデータを出力します

csvファイルの例

有効期限アカウント名 2017年1月2日メアリー4433 2018年12月25日ボブ・1244 2017年1月31日デビッド・1234

コードこれまで

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) 

ありがとうございます!

+0

サンプル入力と希望する出力 – ShmulikA

答えて

1

あなたは、現在の日の3日を追加して、[0]、彼らは印刷にアカウントと一致した場合

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 

サンプル入力

1/28/2017, mary, 4433 
12/25/2018, bob, 1244 
1/31/2017, david, 1234 

出力行の有効期限に対して、その値を比較することができます

4433 
+0

お返事ありがとうございます。あなたの例では1/25/2015の意義は何ですか? – timypcr

+0

はその値を更新しました。コード実行日時を保持する意味は – Shijo

+0

私はあなたのサンプルを実行するのに疲れていて、それは3日以上離れた日付をプリントアウトしていますか? – timypcr

0

はこれとtxtファイルに書き出すための番号での作業、それを手に入れました。皆さん、助けをありがとう!

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() 
関連する問題