如何对比两个列表,忽略每行一半的信息?

发布于 2025-01-17 12:29:29 字数 753 浏览 1 评论 0原文

我需要输出File1中尚未存在的文件1中每个包的列表从SET(File1)中减去SET(file2)。 问题在于,这两个文件的每一行都是filepath以及包装的名称。 ie

file1完美介绍/package.rpm =/home/pasmoti/passoti/storage/package/potpack.rpm

file2 placteridendicenticalInformation/pockage.rpm =/repostorage/repo/repo/package/pockage.rpm

我无法从file1中删除文件路径1,将需要。我无法删除文件路径并将其放回其中,每个文件中都有一些不同的文件path。

到目前为止,代码:

file1 = []
file2 = []
with open('file1.txt') as f1
     file1 = f1.read().splitlines()
with open(file2.txt') as f2
     file2 = f2.read().splitlines()
diff = set(file1) - set(file2)
outfile = open ('Diff.txt','w')
for line in sorted (diff):
     outfile.write(line)
     outfile.write('/n')

这将在“ diff = set -set”行中首先放置一个集合的精确副本,因为它因名称中的filepath而无法从另一行中识别任何单行。

I need to output a list of every package that exists in file1 that does not already exist in file2, but I can't figure out how to get the code to ignore the '=' and every character after it each line when it tries to subtract set(file2) from set(file1).
The problem is that each line of these two files is a filepath along with the name of the package. I.E.

file1 PerfectlyIdenticalInformation/package.rpm=/home/pasmoti/storage/package/package.rpm

file2 PerfectlyIdenticalInformation/package.rpm=/repostorage/repo/package/package.rpm

I can't remove the file path from file1 either, I will need that. I can't remove the file paths and put them back on either, there are a few different filepaths in each file.

The code so far:

file1 = []
file2 = []
with open('file1.txt') as f1
     file1 = f1.read().splitlines()
with open(file2.txt') as f2
     file2 = f2.read().splitlines()
diff = set(file1) - set(file2)
outfile = open ('Diff.txt','w')
for line in sorted (diff):
     outfile.write(line)
     outfile.write('/n')

This outputs a precise copy of whichever set is put first in the "diff = set - set" line, because it does not recognize any single line from the other as being identical due to the filepath in the name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文