繁体   English   中英

从文本文件中创建列表并比较列表

[英]make list from text file and compare the lists

full.txt包含:

www.example.com/a.jpg
www.example.com/b.jpg
www.example.com/k.jpg
www.example.com/n.jpg
www.example.com/x.jpg

partial.txt包含:

a.jpg
k.jpg

为什么以下代码无法提供预期的结果?

with open ('full.txt', 'r') as infile:
        lines_full=[line for line in infile]

with open ('partial.txt', 'r') as infile:
    lines_partial=[line for line in infile]    

with open ('remaining.txt', 'w') as outfile:
    for element in lines_full:
        if element[16:21] not in lines_partial: #element[16:21] means like a.jpg
            outfile.write (element)  

所需的剩余.txt应具有完全位于部分.txt中的完全.txt元素,如下所示:

www.example.com/b.jpg
www.example.com/n.jpg
www.example.com/x.jpg

此代码将在每行末尾包含换行符,这意味着它将永远不会与"a.jpg""k.jpg"精确匹配。

with open ('partial.txt', 'r') as infile:
    lines_partial=[line for line in infile]

更改为

with open ('partial.txt', 'r') as infile:
    lines_partial=[line[:-1] for line in infile]

删除换行符( line[:-1]意思是“没有该行的最后一个字符”)

您可以使用os.path库:

from os import path

with open ('full.txt', 'r') as f:
    lines_full = f.read().splitlines()

with open ('partial.txt', 'r') as f:
    lines_partial = set(f.read().splitlines())  # create set for faster checking

lines_new = [x + '\n' for x in lines_full if path.split(x)[1] not in lines_partial]

with open('remaining.txt', 'w') as f:
    f.writelines(lines_new)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM