使用Python3读取两个文件并写入一个文件

时间:2018-12-18 21:46:37

标签: python python-3.x

我目前在Ubuntu 18.04上使用Python 3。无论如何我都不是程序员,我也不要求进行代码审查,但是,我遇到了一个似乎无法解决的问题。

我有1个名为content.txt的文本文件,正在从中读取行。
我有1个名为standard.txt的文本文件,我正在从中读取行。
我有一个要写入的名为outfile.txt的文本文件。

content = open("content.txt", "r").readlines()
standard = open("standard.txt", "r").readlines()
outfile = "outfile.txt"
outfile_set = set()
with open(outfile, "w") as f:
    for line in content:
        if line not in standard:
            outfile_set.add(line)
    f.writelines(sorted(outfile_set))  

我不确定将以下行放在哪里。我的for循环嵌套可能全部关闭:

f.write("\nNo New Content")  

任何使该工作有效的代码示例将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:0)

我假设如果内容中的每一行都是标准的,则您希望向文件写入“无新内容”。因此,您可以执行以下操作:

with open(outfile, "w") as f:
    for line in content:
        if line not in standard:
            outfile_set.add(line)
    if len(outfile_set) > 0:
        f.writelines(sorted(outfile_set))
    else:
        f.write("\nNo New Content") 

您的原始代码快到了!

答案 1 :(得分:0)

如果我理解的很好,您希望在outfile_set不为空的情况下添加outfile_set或添加字符串“ \ nNo New Content”

替换行

f.writelines(sorted(outfile_set))

if any(outfile_set):
    f.writelines(sorted(outfile_set))
else:
    f.write("\nNo New Content")

答案 2 :(得分:0)

您可以使用set/frozenset来大大减少运行时间:

with open("content.txt", "r") as f:
    content = frozenset(f.readlines())  # only get distinct values from file
with open("standard.txt", "r") as f:
    standard = frozenset(f.readlines()) # only get distinct values from file

# only keep whats in content but not in standard
outfile_set = sorted(content-standard) # set difference, no loops or tests needed


with open ("outfile.txt","w") as outfile:
    if outfile_set:   
        outfile.writelines(sorted(outfile_set))
    else:   
        outfile.write("\nNo New Content")

您可以在此处了解更多信息:


演示:

# Create files
with open("content.txt", "w") as f:
    for n in map(str,range(1,10)):     # use range(1,10,2) for no changes
        f.writelines(n+"\n") 
with open("standard.txt", "w") as f:
    for n in map(str,range(1,10,2)): 
        f.writelines(n+"\n") 

# Process files:
with open("content.txt", "r") as f:
    content = set(f.readlines()) 
with open("standard.txt", "r") as f:
    standard = set(f.readlines())

# only keep whats in content but not in standard
outfile_set = sorted(content-standard)  

with open ("outfile.txt","w") as outfile:
    if outfile_set:
        outfile.writelines(sorted(outfile_set))
    else:
        outfile.write("\nNo New Content")

with open ("outfile.txt") as f:
    print(f.read())

输出:

2
4
6
8

No New Content