如何使用Python按目录对多个值进行排序

2024-04-23 06:31:08 发布

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

我的目录里有540个文件。所有文件的数据格式如下:

文件输入.txt

class    confidence    Xmin        Ymin        Xmax           Ymax
7         0.3456900    89          8            39             53
6         0.0123457    2           1            23             43

文件结果.txt

class    confidence    Xmin        Ymin        Xmax           Ymax
6         0.0123457    2           1            23             43
7         0.3456900    89          8            39             53

我已经解决了读取单个文件的问题。代码如下:

这是我的单文本文件比较代码。它在工作。但我有540个文本文件,我想像这样对文件排序。如何通过同一进程对目录中的多个文件进行排序?我需要为每个文件指定一个特定的文件名。你知道吗

from collections import defaultdict 

maxima = defaultdict(int)

with open('F:\GGR\grnd.txt', 'r') as ifh:
    for line in ifh:
        key, value = line.rsplit(None, 1)
        value = int(value)
        if value > maxima[key]:
            maxima[key] = value

with open('output.txt', 'w') as ofh:
    for key in sorted(maxima):
        ofh.write('{} {}\n'.format(key, maxima[key]))

Tags: 文件key代码目录txt排序valueclass
1条回答
网友
1楼 · 发布于 2024-04-23 06:31:08

使用^{}查找目录下不包含子目录的所有文件:

您的代码已修改:

from collections import defaultdict 
import os

for root, dirs, files in os.walk(r'F:\GGR'):  # this recurses into subdirectories as well
    for f in files:
        maxima = defaultdict(int)
        try:
            with open(os.path.join(root,f)) as ifh:
                for line in ifh:
                    key, value = line.rsplit(None, 1)
                    value = int(value)
                    if value > maxima[key]:
                        maxima[key] = value

            with open(os.path.join(root, f'{f}.out'), 'w') as ofh:
                for key in sorted(maxima):
                    ofh.write('{} {}\n'.format(key, maxima[key]))
        except ValueError:
            # if you have other files in your dir, you might get this error because they 
            # do not conform to the structure of your "needed" files - skip those
            print(f, "Error converting value to int:", value)

如果不需要递归到子目录,请使用os.listdir

更好的解决方案:

直接使用sorted参数对文件内容排序:

from collections import defaultdict 
import os

for root, dirs, files in os.walk(r'./'):
    for f in files:
        print(f)
        maxima = defaultdict(int)
        try:
            with open(os.path.join(root,f)) as ifh, open(
                      os.path.join(root, f'{f}.out'), 'w') as ofh:
                # header
                ofh.write(next(ifh))  
                # data
                ofh.write( '\n'.join(sorted(ifh.readlines(), key = 
                                            lambda x: int(x.split()[-1])))) 
        except ValueError:
            print(f, "Error converting value to int:", ifh)

相关问题 更多 >