在一段时间内获取数据

2024-09-28 03:22:00 发布

您现在位置:Python中文网/ 问答频道 /正文

从给定时间间隔的文本文件中获取数据的方法是什么。你知道吗

搜索.txt

19:00:00 ,  trakjkfsa,
19:00:00 ,  door,
19:00:00 ,  sweater,
19:00:00 ,  sweater,
19:00:00 ,  sweater,
19:00:00 ,  dis,
19:00:01 ,  not,
19:00:01 ,  nokia,
19:00:01 ,  collar,
19:00:01 ,  nokia,
19:00:01 ,  collar,
19:00:01 ,  gsm,
19:00:01 ,  sweater,
19:00:01 ,  sweater,
19:00:01 ,  gsm,
19:00:02 ,  gsm,
19:00:02 ,  show,
19:00:02 ,  wayfreyerv,
19:00:02 ,  door,
19:00:02 ,  collar,
19:00:02 ,  or,
19:00:02 ,  harman,
19:00:02 ,  women's,
19:00:02 ,  collar,
19:00:02 ,  sweater,
19:00:02 ,  head,
19:00:03 ,  womanw,
19:00:03 ,  com.shopclues.utils.k@42233ff0,
19:00:03 ,  samsu,
19:00:03 ,  adidas,
19:00:03 ,  collar,
19:00:04 ,  ambas,

我需要找出19:00:00到19:00:03之间的所有查询 有什么办法可以查出来吗?你知道吗


Tags: 方法txt间隔show时间notgsmdis
2条回答
file = open('search.txt')

start = '19:00:02'
end = '19:00:04'
queries = []     
line = file.read(10)         #read first 10 bytes

while start not in line:   #while the first 10 characters are not '19:00:02'
    file.readline()        
    line = file.read(10)

while end not in line:
    queries.append(file.readline().strip())
    line = file.read(10)

print queries       

这将读取每行的前10个字节,其中包含逗号以下的每个字符。如果字符串19:00:04不在读取字符串中,我将带file.readline().strip()的行的其余部分附加到queries列表中。直到search_for时间被读取为止。你知道吗

使用内置的datetime module

import datetime as dt

t_start = dt.time(19,0,0)
t_end = dt.time(19,0,3)
with open('search.txt') as f:
    for line in f:
        fields = [ x.strip() for x in line.split(',') ]
        timestamp = dt.datetime.strptime(fields[0], "%H:%M:%S").time()

        if t_start < timestamp < t_end:  # use "<=" if you want to search with boundaries included
            print fields[1],

这将打印:

not nokia collar nokia collar gsm sweater sweater gsm gsm show wayfreyerv door collar or harman women's collar sweater head

相关问题 更多 >

    热门问题