简体   繁体   English

如何从文本文件中删除特定行?

[英]How to remove specific lines from a text file?

I have two.txt files.我有两个.txt 文件。 One contains a list of domains (google.com, facebook.com, apple.com, amazon.com), each on a separate line.一个包含域列表(google.z4d236d9a2d102c5fe61c50da4bec50z,z26CAE7718C32180A0A0A0F8E19D6D40A59Z.Z.Z4D236D9A2D9A2D9A2D102C5FE6AD1C501C501C501C502A4BECTAPER6DA4BECREND2AREND2AND2AND.ASPERNANDDANDENDENDENDEND) The other one contains a smaller list of domains (facebook.com, amazon.com), which are also each on a separate line.另一个包含一个较小的域列表(facebook.com,amazon.com),每个域也位于单独的行中。

I want to remove all the domains which are on the second text file, from the first text file.我想从第一个文本文件中删除第二个文本文件中的所有域。

So for example, the first text file would go from例如,第一个文本文件是 go 来自

google.com
facebook.com
apple.com
amazon.com

To:至:

google.com
apple.com

How could this be done with a python script?这怎么能用 python 脚本来完成?

Try with this one.试试这个。

f = open("text1.txt")
list1 = [i.strip() for i in f.readlines()]
f.close()
f = open("text2.txt")
list2 = [i.strip() for i in f.readlines()]
f.close()
def Diff(list1, list2):
    return list(set(list1) - set(list2)) + list(set(list2) - set(list1))
list1 = Diff(list1, list2)
f = open("text1.txt","w")
for i in list1:
    f.write(f"{i}\n")
f.close()

Fast answer and stub:快速回答和存根:

with open('filename1.txt', 'r') as f:
  text1 = f.read()
with open('filename2.txt', 'r') as f:
  text2 = f.read()
with open('filename1.txt', 'w') as f:
  domains = text1.splitlines()
  keep_domains = text2.splitlines()
  new_domains = [d for d in domains if d in keep_domains]
  text = '\n'.join(new_domains)
  f.write(text)

This solution could work fine.该解决方案可以正常工作。

Note: I dislike method.readlines() of file object, I prefer string method.splitlines because first one keeps new line characters.注意:我不喜欢 object 文件的 method.readlines(),我更喜欢字符串 method.splitlines,因为第一个保留换行符。 I dislike to use 'r+' mode when reading/writing file, I prefer to make it separately.我不喜欢在读/写文件时使用'r+'模式,我更喜欢单独制作。

Python Diff To get a diff using the difflib library, you can simply call the united_diff function on it. Python Diff要使用 difflib 库获取 diff,您只需调用 united_diff function 即可。 For example, Lets say you have 2 files, file1 and file2 with the following content: Now to take their diff use the following code: This will give the output:例如,假设您有 2 个文件,file1 和 file2,其内容如下: 现在使用以下代码获取它们的差异:这将给出 output:

def Diff(li1, li2):
    return list(set(li1) - set(li2)) + list(set(li2) - set(li1))

li1 = ['google.com','facebook.com','apple.com','amazon.com']
li2 = ['facebook.com','amazon.com']

print(Diff(li1, li2))

Compare Two Text Files比较两个文本文件

file1 = "path to file"
file2 = "path to file"

import difflib
with open('file1') as f1:
    f1_text = f1.read()
with open('file2') as f2:
    f2_text = f2.read()
# Find and print the diff:
for line in difflib.unified_diff(f1_text, f2_text, fromfile='file1', tofile='file2', lineterm=''):
    print line

在此处输入图像描述

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

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