【问题标题】:Merging and Comparing合并和比较
【发布时间】:2020-06-15 13:33:45
【问题描述】:

有一个包含文件的目录:

ab_list
bd_list
cd_list
mno_list
hk_list 
pd_list

我在此目录之外还有另一个名为 testfile 的文件:

abc
que nw

ab_list   ON   8
gs_list   ON   9
hk_list   OFF  9
bd_list   ON   7
cd_list   OFF  6
fr_list   ON   5
mno_list  ON   4
pq_list   OFF   6
jk_list   ON   7
pd_list   OFF  8

我想比较 2 和所有带有文件名和 ON 的文件(如果匹配),它们的内容应该合并到一个名为 merge_file 的新文件中。与 testfile 匹配但为 OFF 的其他文件,其文件名应打印在 new_file 中。 ab_list bd_list 和 mno_list 的内容应该合并到 top_file 输出应该像 新文件:

cd_list OFF no.of lines in file
pd_list OFF no.of lines in file
hk_list OFF no. of lines in file
merge_file (this has all ON merged) no.of lines in file

这是到目前为止的代码:

from pathlib import Path

with open('testfile') as fp:
    data = dict([tuple(line.split())for line in fp if line.strip()])

with open('merge_file', 'w') as merge_file, open('match_file', 'w') as match_file:
    lines = 0
    for fp in Path(r'./test').glob('*_list'):
        if fp.name in data:
            if data[fp.name] == 'ON':
                content = fp.open().readlines()
                lines += len(content)

                merge_file.write('\n'.join(content) + '\n')
            else:
                content = fp.open().readlines()
                match_file.write(fp.name + ' OFF {}\n'.format(len(content)))
    match_file.write('merge_file (this has all ON merged) {}'.format(lines))

我想从第一行读取,但它给出了一个名为 Index Error: list out of range 的错误。当前代码从第 4 行读取。

【问题讨论】:

    标签: python python-3.x merge


    【解决方案1】:

    假设目录名称是Folder,并且在该目录中有另一个名为folder 的目录,这段代码就是这样做的:

    from glob import glob
    
    test_file_directory = "C:\\Users\\User\\Desktop\\Folder\\"
    
    files1 = glob("*.txt")
    with open(test_file_directory+"testfile.txt","r") as f:
        files2 = [' '.join([l.split()[0],l.split()[1]]) for l in f.readlines()[3:]]
    
    for f1 in files1:
        for f2 in files2:
            if f1[:-4]+'   ON' == f2:
                #print('match')
                with open('merge_file.txt','a') as a:
                    with open(f1,'r') as r:
                        a.write(r.read()+'\n')
            elif f1[:-4]+'   OFF' == f2:
                #print('match')
                with open('match_file.txt','a') as a:
                    with open(f1,'r') as r:
                        a.write(f"{f2} {len(r.readlines())}\n")
    

    【讨论】:

    • 评论不用于扩展讨论;这个对话是moved to chat
    • 嗨,在这段代码中,我遇到了一些不以行 [3:] 开头的文件,起始行可以是可变的,当我删除 [3:] 时,它给出了一个错误提示索引错误超出范围
    • 你能帮帮我吗
    • 是的,你的问题在哪里?
    • 我正在考虑添加files2 = [' '.join([l.split()[0],l.split()[1]]) for l in f.readlines() if len(l) > 1] but that gives IndexError: list index out of range
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多