逐行读取多个文件,如果时间不相等,则转到一个文件中的下一行(python3)

2024-10-04 05:33:13 发布

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

我想逐行读取多个文件,如果该行的时间戳与其他文件的时间戳不符,我想跳到该文件的下一行,同时保持另一个文件在同一行直到它们匹配。在

read two files
for file0line, file1line in zip(file0, file1):
    # compare time file0line with time file1line
    if file0line < file1line:
        next file0line
        # compare again while file1line stay the same
    if file1line < file0line:
        next file1line
        # compare again while file0line stay the same
    else:
        # they are synced in time

我的代码:

^{pr2}$

文件示例1

Time,Accel-X (g), Accel-Y (g), Accel-Z (g)
2017-03-17 15:41:20.534,0.468750,0.734375,-0.625000
2017-03-17 15:41:20.545,0.218750,0.437500,0.921631
2017-03-17 15:41:20.555,0.281006,0.375000,0.999756
2017-03-17 15:41:20.565,0.343506,0.296875,1.031006
2017-03-17 15:41:20.575,0.328125,0.296875,1.031006
2017-03-17 15:41:20.584,0.328125,0.281250,1.046631
2017-03-17 15:41:20.594,0.296875,0.281006,1.031250
2017-03-17 15:41:20.604,0.281250,0.312256,1.031006
2017-03-17 15:41:20.614,0.250000,0.327881,1.046631
2017-03-17 15:41:20.625,0.249756,0.327881,1.031250
2017-03-17 15:41:20.635,0.250000,0.343506,1.031006
2017-03-17 15:41:20.645,0.249756,0.374756,1.031250
2017-03-17 15:41:20.655,0.265381,0.421631,1.000000
2017-03-17 15:41:20.665,0.281250,0.453125,0.984131
2017-03-17 15:41:20.674,0.296875,0.484375,0.921631
2017-03-17 15:41:20.684,0.312500,0.515625,0.921631
2017-03-17 15:41:20.694,0.328125,0.562500,0.921875
2017-03-17 15:41:20.704,0.359375,0.593750,0.937500
2017-03-17 15:41:20.715,0.375000,0.625000,0.937256
2017-03-17 15:41:20.725,0.406250,0.656250,0.890381

文件示例2

Time,Accel-X (g), Accel-Y (g), Accel-Z (g)
2017-03-17 15:41:16.514,-0.781250,0.250000,0.500000
2017-03-17 15:41:16.524,-0.468750,-0.234131,0.499756
2017-03-17 15:41:16.534,-0.484131,-0.250000,0.515625
2017-03-17 15:41:16.545,-0.499756,-0.218750,0.484375
2017-03-17 15:41:16.555,-0.515381,-0.203125,0.390625
2017-03-17 15:41:16.565,-0.500000,-0.156250,0.437256
2017-03-17 15:41:16.575,-0.468750,-0.171631,0.468506
2017-03-17 15:41:16.584,-0.452881,-0.156250,0.468506
2017-03-17 15:41:16.594,-0.515381,-0.125000,0.515381
2017-03-17 15:41:16.604,-0.546631,-0.124756,0.515381
2017-03-17 15:41:16.614,-0.546875,-0.156006,0.577881
2017-03-17 15:41:16.625,-0.500000,-0.202881,0.656006
2017-03-17 15:41:16.635,-0.468750,-0.265381,0.687256
2017-03-17 15:41:16.645,-0.452881,-0.312500,0.671875
2017-03-17 15:41:16.655,-0.437500,-0.375000,0.703125
2017-03-17 15:41:16.665,-0.452881,-0.437500,0.718750
2017-03-17 15:41:16.674,-0.437500,-0.500000,0.781250
2017-03-17 15:41:16.684,-0.468750,-0.484131,0.796875
2017-03-17 15:41:16.694,-0.500000,-0.468506,0.796875
2017-03-17 15:41:16.704,-0.500000,-0.421631,0.843750

Tags: 文件theiniftime时间nextcompare
1条回答
网友
1楼 · 发布于 2024-10-04 05:33:13

Zip将始终从每个生成器中选择一个结果-无论是文件还是CSV读取器。在

因此,这是一个问题,为每一组行,检查最大时间抽头,如果任何一行有一个较小的日期时间,只读该行,直到所有行的时间戳相等。在

我为n个文件编写了代码,其中任何一个文件都允许在前面——所以乍看起来比你的版本要复杂一些。但是,将其封装在生成器中可以实现非常简单的用法:

def getdate(f):
    return datetime.strptime(f, "%Y-%m-%d %H:%M:%S.%f")

def sync_line_reader(*readers, skip_lines=1):
    def get_one_line_of_each(readers):
        lines = []
        for reader in readers:
            lines.append(next(reader))


    try:
        #read first lines, skipping the header:
        for _ in range(skip_lines):
            lines = get_one_line_of_each(*readers)
        while True:
            max_date = max(get_date(f[0]) for f in lines)
            for i, line in enumerate(lines[:]):
                while get_date(line[0]) < max_date:
                    lines[i] = line = next(readers[i])
            yield lines
            lines = get_one_line_of_each(readers)    
    except StopIteration:
        return

您可以将其插入代码:

^{pr2}$

相关问题 更多 >