【问题标题】:Reading multiple files sequentially group wise按组顺序读取多个文件
【发布时间】:2021-09-10 12:47:32
【问题描述】:

我在一个目录中有如下数据

 IU.WRT.00.MTR.1999.081.081015.txt
 IU.WRT.00.MTS.2007.229.022240.txt
 IU.WRT.00.MTR.2007.229.022240.txt
 IU.WRT.00.MTT.1999.081.081015.txt
 IU.WRT.00.MTS.1999.081.081015.txt
 IU.WRT.00.MTT.2007.229.022240.txt

首先我想通过使用 3 个文件的相似模式(由 R、S、T 不同)对数据进行分组,如下所示:

IU.WRT.00.MTR.1999.081.081015.txt
IU.WRT.00.MTS.1999.081.081015.txt
IU.WRT.00.MTT.1999.081.081015.txt

并想对其应用一些操作

然后我想读取数据

IU.WRT.00.MTT.2007.229.022240.txt
IU.WRT.00.MTS.2007.229.022240.txt
IU.WRT.00.MTR.2007.229.022240.txt 

并想对其应用类似的操作。

同样,我想继续处理数百万个数据集。

我尝试了示例脚本

import os
import glob
import matplotlib.pyplot as plt
from collections import defaultdict

def groupfiles(pattern):
    files = glob.glob(pattern)
    filedict = defaultdict(list)
    for file in files:
        parts = file.split(".")
        filedict[".".join([parts[5], parts[6], parts[7]])].append(file)
    for filegroup in filedict.values():
        yield filegroup
 
for relatedfiles in groupfiles('*.txt'):
    print(relatedfiles)

    for filename in relatedfiles:
        print(filename)

    

但是它一个接一个地读取文件,但每次我需要一次读取 3 个文件(即通过采用顺序标准,首先它会读取前三个文件,然后读取接下来的三个文件,依此类推。我希望专家可以帮助我。在此先感谢。

【问题讨论】:

    标签: python pandas numpy glob


    【解决方案1】:
    1. 按多个键对文件列表进行排序。
    import os
    files = [f for f in os.listdir("C:/username/folder") if f.endswith(".txt")]
    grouped = sorted(files, key=lambda x: (x.split(".")[4:6], x.split(".")[3]))
    
    >>> grouped
    ['IU.WRT.00.MTR.1999.081.081015.txt',
     'IU.WRT.00.MTS.1999.081.081015.txt',
     'IU.WRT.00.MTT.1999.081.081015.txt',
     'IU.WRT.00.MTR.2007.229.022240.txt',
     'IU.WRT.00.MTS.2007.229.022240.txt',
     'IU.WRT.00.MTT.2007.229.022240.txt']
    
    1. 使用来自itertoolsgrouper 配方以三个为单位遍历排序列表。
    from itertools import zip_longest
    def grouper(iterable, n, fillvalue=None):
        "Collect data into fixed-length chunks or blocks"
        # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
        args = [iter(iterable)] * n
        return zip_longest(*args, fillvalue=fillvalue)
    
    for f in grouper(grouped, 3): #f is a tuple of three file names
        #your file operations here
    

    【讨论】:

    • “随机读取文件”是什么意思?我按要求的顺序排序并将其保存到grouped,然后三人一组地遍历grouped。这就是我认为你需要的?查看grouped的输出
    • @chumunB - 您文件夹中的所有“.txt”文件是否具有完全相同的命名约定?如果是这样,请查看我编辑的代码以编程方式获取files 列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多