用于键入文件的日期范围并重定向输出的python脚本

时间:2016-11-24 12:21:40

标签: python

我有一个输入文件1.csv

42200000000099920160613000000 column=h:profile, timestamp=1465780825878, value=[{"weight":100.0,"centroidLatitude":23.723663502974482,"centroidLongitude":58.812344445144724,"centroidUncertainity":1.384228745747496E11,:{"Wed21":100.0,"Wed20":100.0,"Wed23":100.0,"Wed22":100.0,"Wed14":100.0,"Wed13":100.0,"Wed16":100.0,"Wed15":100.0,"Wed18":100.0,"Wed17":100.0,"Wed19":100.0
42200000000099920160613000000 column=h:profile, timestamp=1465780825878, value=[{"weight":100.0,"centroidLatitude":23.723663502974482,"centroidLongitude":58.812344445144724,"centroidUncertainity":1.384228745747496E11,:{"Wed21":100.0,"Wed20":100.0,"Wed23":100.0,"Wed22":100.0,"Wed14":100.0,"Wed13":100.0,"Wed16":100.0,"Wed15":100.0,"Wed18":100.0,"Wed17":100.0,"Wed19":100.0
42200000000099920160614000000 column=h:profile, timestamp=1465780825878, value=[{"weight":100.0,"centroidLatitude":23.723663502974482,"centroidLongitude":58.812344445144724,"centroidUncertainity":1.384228745747496E11,:{"Wed21":100.0,"Wed20":100.0,"Wed23":100.0,"Wed22":100.0,"Wed14":100.0,"Wed13":100.0,"Wed16":100.0,"Wed15":100.0,"Wed18":100.0,"Wed17":100.0,"Wed19":100.0,
42200000000099920160615000000 column=h:profile, timestamp=1465780825878, value=[{"weight":100.0,"centroidLatitude":23.723663502974482,"centroidLongitude":58.812344445144724,"centroidUncertainity":1.384228745747496E11,:{"Wed21":100.0,"Wed20":100.0,"Wed23":100.0,"Wed22":100.0,"Wed14":100.0,"Wed13":100.0,"Wed16":100.0,"Wed15":100.0,"Wed18":100.0,"Wed17":100.0,"Wed19":100.0

我想仅从20160613到20160614获取记录。

我写的程序,我需要在代码中输入开始日期和结束日期,而不是我要求用户输入开始日期和结束日期并将输出重定向到文件

import re
import datetime

# please input from console i have used fixed values
lbound_date = datetime.datetime.strptime("20160613", "%Y%m%d")
rbound_date = datetime.datetime.strptime("20160614", "%Y%m%d")


t= open("1.csv")
p = re.compile(r'^[0-9]{15}(?P<date>[0-9]{8})')

out = []


for line in t:
    s = p.match(line)
if s:
    sdate_str = s.group("date")
    # sort the dates to get only ones between range
    sdate = datetime.datetime.strptime(sdate_str, "%Y%m%d")
    if sdate >= lbound_date and sdate <= rbound_date:
        out.append(line)


for lineout in out:
    print (lineout)

1 个答案:

答案 0 :(得分:0)

缩进是错误的,所以你的循环没有任何用处。最后只处理最后一行(s是最后一行的匹配):

修复:从第二行正确缩进:

for line in t:
    s = p.match(line)
    if s:
        sdate_str = s.group("date")
        # sort the dates to get only ones between range
        sdate = datetime.datetime.strptime(sdate_str, "%Y%m%d")
        if sdate >= lbound_date and sdate <= rbound_date:
            out.append(line)

(抱歉这么简单的回答)