Python 如何将csv(字符串)中的日期与实际日期进行比较 filenameA=“ApptA.csv” filenameAc=“CheckoutA.csv” def checkouttenata(): 全局文件名a 全局文件名AC 导入csv 导入日期时间 将open(filenameA,'r')作为inp,将open(filenameAc,'a',newline=“”)作为out: my_writer=csv.writer(out) 对于csv.reader(inp)中的行: my_date=datetime.date.today() string\u date=my\u date.strftime(“%d/%m/%Y”) 如果第[5]行
正常,那么还有一些改进需要做,我将对其进行编辑,但是您要将今天的日期转换为一个字符串,使用Python 如何将csv(字符串)中的日期与实际日期进行比较 filenameA=“ApptA.csv” filenameAc=“CheckoutA.csv” def checkouttenata(): 全局文件名a 全局文件名AC 导入csv 导入日期时间 将open(filenameA,'r')作为inp,将open(filenameAc,'a',newline=“”)作为out: my_writer=csv.writer(out) 对于csv.reader(inp)中的行: my_date=datetime.date.today() string\u date=my\u date.strftime(“%d/%m/%Y”) 如果第[5]行,python,csv,date,Python,Csv,Date,正常,那么还有一些改进需要做,我将对其进行编辑,但是您要将今天的日期转换为一个字符串,使用strftime()比较这两个字符串,您应该将字符串日期从csv文件转换为datetime对象,并进行比较 我将添加大量注释,试图解释代码及其背后的原因 filenameA ="ApptA.csv" filenameAc = "CheckoutA.csv" def checkouttenantA(): global filenameA global filenameAc impo
strftime()
比较这两个字符串,您应该将字符串日期从csv文件转换为datetime
对象,并进行比较
我将添加大量注释,试图解释代码及其背后的原因
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)
#导入应位于顶部
导入csv
#请注意,我们正在从datetime导入datetime(我们正在从模块datetime导入'datetime'类型)
从日期时间导入导入日期时间
#尽可能避免使用globals(这里不需要)
在csv(输入文件路径)中定义检查日期:
''加载csv文件并将日期与今天的日期进行比较的函数''
#创建一个列表来存储符合我们标准的行
#将行追加到此列表将生成列表列表(嵌套列表)
输出数据=[]
#在循环之前获取todays date以避免每行调用now()
#我们只需要这一次,它会减慢每行调用它的循环速度
todays\u date=datetime.now()
#使用函数参数在此处打开csv
打开(输入文件路径、输出文件路径)作为csv文件:
reader=csv.reader(csv\u文件)
#在行上迭代并获取每行中的日期
对于读取器中的行:
字符串\日期=行[5]
#将字符串转换为datetime对象
csv_date=datetime.strtime(字符串_date,“%d/%m/%Y”)
#比较日期,如果符合条件,则追加
如果csv_date我建议用于此类任务:
# 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)
将熊猫作为pd导入
filenameA=“ApptA.csv”
filenameAc=“CheckoutA.csv”
今天=pd.datetime.today()
df=pd.read\u csv(filenameA,parse\u dates=[5])
df.loc[df.iloc[:,5]请添加csv内容的小样本这里是csv内容的样本
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)