gpt4 book ai didi

php - 如何使用 perl/php/grep/etc 从 csv 中提取日期范围?

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:26 24 4
gpt4 key购买 nike

有没有办法像下面这样获取文本(如果它已经在数组或文件中)并让它去除具有指定日期范围的行?

例如,如果我想要从 2009-09-04 到 2009-09-09 的每一行都被拉出(也许这可以用 grep 来完成?)我将如何去做?

date,test,time,avail
2009-09-01,JS,0.119,99.90
2009-09-02,JS,0.154,99.89
2009-09-03,SWF,0.177,99.90
2009-09-04,SWF,0.177,99.90
2009-09-05,SWF,0.177,99.90
2009-09-06,SWF,0.177,99.90
2009-09-07,SWF,0.177,99.90
2009-09-08,SWF,0.177,99.90
2009-09-09,SWF,0.177,99.90
2009-09-10,SWF,0.177,99.90

谢谢!

最佳答案

python

import csv
import datetime

start= datetime.datetime(2009,9,4)
end= datetime.datetime(2009,9,9)

source= csv.DictReader( open("someFile","rb") )
for row in source:
dt = datetime.datetime.strptime(row['date'],"%Y-%m-%d")
if start <= dt <= end:
print row # depends on what "pulled out" means

关于php - 如何使用 perl/php/grep/etc 从 csv 中提取日期范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1369287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com