循环通过csv文件并根据条件获取数据

2024-05-14 09:46:28 发布

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

我有一个问题,我没有找到任何解决方案,在所有的CSV职位我看。 我有几千行的csv,第一列有日期和时间戳。 每2秒就有一个新的时间戳

注1:非常重要的一点(这导致了我的问题)是每个日期和时间出现几次

注2:日期已排序

我的前40排

30/07/2018 22:52:52,4,50,26

30/07/2018 22:52:52,7,49,26

30/07/2018 22:52:52,6,50,26

30/07/2018 22:52:52,5,51,26

30/07/2018 22:52:52,2,50,26

30/07/2018 22:52:52,3,49,26

30/07/2018 22:52:55,4,50,26

30/07/2018 22:52:55,7,49,26

30/07/2018 22:52:55,6,50,26

30/07/2018 22:52:55,5,51,26

30/07/2018 22:52:55,2,50,26

30/07/2018 22:52:55,3,49,26

30/07/2018 22:52:57,4,50,26

30/07/2018 22:52:57,7,49,26

30/07/2018 22:52:57,6,50,26

30/07/2018 22:52:57,5,51,26

30/07/2018 22:52:57,2,50,26

30/07/2018 22:52:57,3,49,26

30/07/2018 22:52:59,4,50,26

30/07/2018 22:52:59,7,49,26

30/07/2018 22:52:59,6,50,26

30/07/2018 22:52:59,5,51,26

30/07/2018 22:52:59,2,50,26

30/07/2018 22:52:59,3,49,26

30/07/2018 22:53:02,4,50,26

30/07/2018 22:53:02,7,49,26

30/07/2018 22:53:02,6,50,26

30/07/2018 22:53:02,5,51,26

30/07/2018 22:53:02,2,50,26

30/07/2018 22:53:02,3,49,26

30/07/2018 22:53:04,4,50,26

30/07/2018 22:53:04,7,49,26

30/07/2018 22:53:04,6,50,26

30/07/2018 22:53:04,5,51,26

30/07/2018 22:53:04,2,50,26

30/07/2018 22:53:04,3,49,26

30/07/2018 22:53:07,4,50,26

30/07/2018 22:53:07,7,49,26

30/07/2018 22:53:07,6,50,26

30/07/2018 22:53:07,5,51,26

30/07/2018 22:53:07,2,50,26

30/07/2018 22:53:07,3,49,26

30/07/2018 22:53:09,4,50,26

30/07/2018 22:53:09,7,49,26

30/07/2018 22:53:09,6,50,26

30/07/2018 22:53:09,5,50,26

30/07/2018 22:53:09,2,50,26

30/07/2018 22:53:09,3,49,26

我需要从用户示例5中获取一个输入,然后每隔5秒获取最后一个时间戳,并用第2列和第3列生成字典。 因此,对于输入5,我必须采取行:

30/07/2018 22:52:59,4,50,26

30/07/2018 22:52:59,7,49,26

30/07/2018 22:52:59,6,50,26

30/07/2018 22:52:59,5,51,26

30/07/2018 22:52:59,2,50,26

30/07/2018 22:52:59,3,49,26

30/07/2018 22:53:09,7,49,26

30/07/2018 22:53:09,6,50,26

30/07/2018 22:53:09,5,50,26

30/07/2018 22:53:09,2,50,26

30/07/2018 22:53:09,3,49,26

字典应该是这样的:

{timestamp : {2nd column : 3rd columns}}

{30/07/2018 22:52:59: {4:50,7:49,6:50,5:51,2:50,3:49}}

到目前为止,我所拥有的每一个时间戳只能使用1次,这意味着我每一个时间戳都会得到一本字典:

{30/07/2018 22:52:59: {4:50}, 30/07/2018 22:53:09:{4:50}}

这是我的密码:

with open(os.path.join(inputPath,filename),"r") as f:
            dictTemp = {}
            r = csv.reader(f)
            #Gets first date from node file
            minTime = dt.strptime(next(r)[0], "%d/%m/%Y %H:%M:%S")
        #open file second time to loop through all rows
            for line in r:
                currentTime = dt.strptime(line[0], "%d/%m/%Y %H:%M:%S")
                if((currentTime-minTime).total_seconds() > 5):
                    minTime = currentTime
                    scenariotimeStamps.append((currentTime.strftime("%Y%m%d%H%M%S")))
                    dictTemp[line[1]] = line[2]
                    dicComplete[str(currentTime.strftime("%Y%m%d%H%M%S"))] = dictTemp

Tags: csv字典line时间dt职位open解决方案
1条回答
网友
1楼 · 发布于 2024-05-14 09:46:28

使用:

dictTemp[line[1]] = line[2]
dicComplete[str(currentTime.strftime("%Y%m%d%H%M%S"))] = dictTemp

在每次迭代中都要覆盖dictdicComplete[str(currentTime.strftime("%Y%m%d%H%M%S"))]。将两行更改为:

dictComplete.setdefault(str(currentTime.strftime("%Y%m%d%H%M%S")), {})[line[1]] = line[2]

而且,由于您希望在验证距离最后一个时间戳至少5秒后获取相同时间戳的所有行,而不是:

if((currentTime-minTime).total_seconds() > 5):

如果currentTime等于minTime,则应该允许:

if currentTime == minTime or (currentTime-minTime).total_seconds() > 5:

相关问题 更多 >

    热门问题