如何用Python对目录中的倍数值进行排序
我的目录中有540个文件。所有文件的数据格式如下所示: 文件输入.txt 文件result.txt 我通过读取单个文件解决了这个问题。代码如下: 这是我的单文本文件比较代码。它起作用了。但是我有540个文本文件,我想像这样对文件进行排序。如何通过同一进程对目录中的多个文件进行排序?我需要为每个文件指定一个特定的文件名如何用Python对目录中的倍数值进行排序,python,file,Python,File,我的目录中有540个文件。所有文件的数据格式如下所示: 文件输入.txt 文件result.txt 我通过读取单个文件解决了这个问题。代码如下: 这是我的单文本文件比较代码。它起作用了。但是我有540个文本文件,我想像这样对文件进行排序。如何通过同一进程对目录中的多个文件进行排序?我需要为每个文件指定一个特定的文件名 from collections import defaultdict maxima = defaultdict(int) with open('F:\GGR\grnd.tx
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]))
用于查找目录下未包含子目录的所有文件:
您的代码已调整:
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)
如果不需要递归到子曲面中,请使用
更好的解决方案:
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)
直接使用排序的的键参数对文件内容进行排序
:
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)
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)