简体   繁体   English

如何使用 Python 基于一个文件对一组文件的内容进行排序

[英]How to order the content of a set of files based on one file with Python

I'm stuck in a problem regarding strings order inside 3 text files.我遇到了有关 3 个文本文件中字符串顺序的问题。 Here below and example:下面是示例:

file_A.txt

ID    value
AAA   1
BBB   2
CCC   3

file_B.txt

ID    value
BBB   2
AAA   1
CCC   3

file_C.txt

ID    value
CCC   3
AAA   1
BBB   2

As you can see, all the 3 files contain strings and these strings are the same for each file but in different order.如您所见,所有 3 个文件都包含字符串,并且这些字符串对于每个文件都是相同的,但顺序不同。 I'd like to use the fileA.txt strings order as footprint for the other files in order to modify fileB.txt and fileC.txt like this:我想使用fileA.txt字符串顺序作为其他文件的足迹,以便像这样修改fileB.txtfileC.txt

file_A.txt

ID    value
AAA   1
BBB   2
CCC   3

file_B.txt

ID    value
AAA   1
BBB   2
CCC   3

file_C.txt

ID    value
AAA   1
BBB   2
CCC   3

Thanks for tips and help.感谢提示和帮助。

Given these files:鉴于这些文件:

$ head *.txt
==> file_a.txt <==
ID    value
AAA   1
CCC   3
BBB   2

==> file_b.txt <==
ID    value
BBB   2
AAA   1
CCC   3

==> file_c.txt <==
ID    value
CCC   3
AAA   1
BBB   2

You could do something like this:你可以这样做:

from pathlib import Path 

p=Path(path_to_files)
id_file=Path('path_to_files/file_a.txt')

# create an index from file_a:
with open(id_file) as f:
    next(f)
    idx={line.split()[0]:i for i, line in enumerate(f)}

# for the other files, use that index to sort
for fn in p.glob('file_[bc].txt'):  # glob could be 'file_[!a].txt' too
    with open(fn, 'r+') as f:
        dat=f.readlines()
        f.seek(0)
        for line in [dat[0]]+sorted(dat[1:], 
                             key=lambda l: idx.get(l.split()[0],0)):
            f.write(line)

Result:结果:

$ head *.txt
==> file_a.txt <==
ID    value
AAA   1
CCC   3
BBB   2

==> file_b.txt <==
ID    value
AAA   1
CCC   3
BBB   2

==> file_c.txt <==
ID    value
AAA   1
CCC   3
BBB   2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:根据一个顺序对两个文件进行排序 - Python: Sorting a two files based on the order of one Python:如何根据文件名迭代一组文件? - Python: How to iterate through set of files based on file names? 如何根据内容批量重命名文件 python - How to bulk rename files based on their content in python 基于条件在Pandas、Python中编辑schedule文件的顺序和内容 - Editing the order and content of schedule file in Pandas, Python based on condition 如何根据python中的特定条件将多个文本文件的内容附加到新的文本文件中? - How can I append the content of multiple text file to a new text files based on specific conditions in python? Python3如何根据行内容将大文本文件拆分为较小的文件 - Python3 How to split a large text file into smaller files based on line content Python append 多个文件按给定顺序合并为一个大文件 - Python append multiple files in given order to one big file 如何根据其内容将一组图像文件群集到不同的文件夹 - How to cluster a set of image files to different folders based on their content Python-根据文件内容分离文件 - Python - separate files based on their content 如何在python中将另一个文件的内容附加到一个文件的内容 - how to append the content of one file at the of the content of another file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM