2017-08-19 11 views
0
filenameA ="ApptA.csv" 
filenameAc = "CheckoutA.csv" 

def checkouttenantA(): 
    global filenameA 
    global filenameAc 
    import csv 
    import datetime 
    with open(filenameA, 'r') as inp, open(filenameAc, 'a' , newline = "") as out: 
     my_writer = csv.writer(out) 
     for row in csv.reader(inp): 
      my_date= datetime.date.today() 
      string_date = my_date.strftime("%d/%m/%Y") 
      if row[5] <= string_date: 
       my_writer.writerow(row) 

日付は、[5]列のExcelファイルで%d /%m /%Y形式で保存されます。私は実際の日付とcsvファイルの日付を比較しようとしていますが、%d部分だけを比較しています。私は日付が文字列形式であるためだと思います。日付をcsv(文字列)から実際の日付に比較する方法

+0

のCSVコンテンツ – PRMoureu

+0

の小さなサンプルを追加してくださいhttps://pastebin.com/pN7pVdUNここでのCSVコンテンツのサンプルです –

答えて

2

[OK]このようにいくつかの改良が加えられましたが、私はこれを編集しますが、今日の日付をstrftime()の文字列に変換して2つの文字列を比較すると、文字列の日付をcsvファイルからdatetimeオブジェクトに変更し、それらを代わりに比較します。

私は、コードとその背後にある推論について説明するために、たくさんのコメントを追加します。

# imports should go at the top 
import csv 

# notice we are importing datetime from datetime (we are importing the `datetime` type from the module datetime 
import from datetime import datetime 

# try to avoid globals where possible (they're not needed here) 

def check_dates_in_csv(input_filepath): 
    ''' function to load csv file and compare dates to todays date''' 

    # create a list to store the rows which meet our criteria 
    # appending the rows to this will make a list of lists (nested list) 
    output_data = [] 

    # get todays date before loop to avoid calling now() every line 
    # we only need this once and it'll slow the loop down calling it every row 
    todays_date = datetime.now() 

    # open your csv here using the function argument 
    with open(input_filepath, output_filepath) as csv_file: 
     reader = csv.reader(csv_file) 

     # iterate over the rows and grab the date in each row 
     for row in reader: 
      string_date = row[5] 

      # convert the string to a datetime object 
      csv_date = datetime.strptime(string_date, '%d/%m/%Y') 

      # compare the dates and append if it meets the criteria 
      if csv_date <= todays_date: 
       output_data.append(row) 

     # function should only do one thing, compare the dates 
     # save the output after 
     return output_data 

# then run the script here 
# this comparison is basically the entry point of the python program 
# this answer explains it better than I could: https://stackoverflow.com/questions/419163/what-does-if-name-main-do 
if __name__ == "__main__": 

    # use our new function to get the output data 
    output_data = check_dates_in_csv("input_file.csv") 

    # save the data here 
    with open("output.csv", "w") as output_file: 
     writer = csv.writer(output_file) 
     writer.writerows(output_data) 
0

私は、このようなタスクのためPandasを使用することをお勧めします:

import pandas as pd 

filenameA ="ApptA.csv" 
filenameAc = "CheckoutA.csv" 
today = pd.datetime.today() 

df = pd.read_csv(filenameA, parse_dates=[5]) 
df.loc[df.iloc[:, 5] <= today].to_csv(filenameAc, index=False) 
関連する問題