从文本文件制作列表并比较列表

时间:2013-09-13 05:58:20

标签: python

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)  

所需的remaining.txt应该具有full.txt中不在partial.txt中的那些元素,如下所示:

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

2 个答案:

答案 0 :(得分:1)

此代码将在每行末尾包含换行符,这意味着它将永远不会与"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]表示“没有行的最后一个字符”)

答案 1 :(得分: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)