相关疑难解决方法(0)

在Python中使用difflib比较两个.txt文件

我试图比较两个文本文件并输出比较文件中的第一个字符串,该字符串不匹配但由于我是python的新手而遇到困难.有人可以给我一个使用这个模块的示例方法.

当我尝试这样的事情:

result = difflib.SequenceMatcher(None, testFile, comparisonFile)
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,说'file'类型的对象没有len.

python difflib

21
推荐指数
3
解决办法
7万
查看次数

Python:比较两个csv文件并打印出差异

我需要比较两个CSV文件并在第三个CSV文件中打印出差异.在我的例子中,第一个CSV是名为old.csv的旧哈希列表,第二个CSV是包含旧哈希和新哈希的新哈希列表.

这是我的代码:

import csv
t1 = open('old.csv', 'r')
t2 = open('new.csv', 'r')
fileone = t1.readlines()
filetwo = t2.readlines()
t1.close()
t2.close()

outFile = open('update.csv', 'w')
x = 0
for i in fileone:
    if i != filetwo[x]:
        outFile.write(filetwo[x])
    x += 1
outFile.close()
Run Code Online (Sandbox Code Playgroud)

第三个文件是旧文件的副本,而不是更新.怎么了 ?我希望你能帮助我,非常感谢!!

PS:我不想使用差异

python csv

10
推荐指数
4
解决办法
4万
查看次数

如何检测Python中的两个文件是否相同

正在进行系统调用"md5sum file1"和"md5sum file2"并在这种情况下比较两个返回值吗?

python md5 compare file

6
推荐指数
4
解决办法
8080
查看次数

How to diff the two files using Python Generator

I have one file of 100GB having 1 to 1000000000000 separated by new line. In this some lines are missing like 5, 11, 19919 etc. My Ram size is 8GB.

How to find the missing elements.

My idea take another file for i in range(1,1000000000000) read the lines one by one using the generator. can we use yield statement for this

Can help in writing the code

My Code, the below code taking as a list in does the …

python generator range

6
推荐指数
2
解决办法
271
查看次数

Python - 如何比较两个文件并仅输出第三个文件中的不同行

我在 SO 上搜索过类似的问题,但没有找到任何对我有用的东西。

我有两个大文件,它们应该是相同的,但其中一个文件比另一个文件长 60 行。我想知道这些线路是什么以及在哪里可以找到它们。

我读过可以使用 difflib 来做到这一点,但我不知道如何去做。我总是在文件中找到+和,但我不想要这样。-我只想扫描这两个文件并将不常见的 60 行报告到第三个文件中。

我编写了这段代码,但它没有打印出不同的行。

f1 = open('file1.txt','r')
f2 = open('file2.txt','r')
f3 = open('file3.txt','w')

diff = set(f1).difference(f2)
same.discard('\n')

for line in same:
    f3.write(line)
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×5

compare ×1

csv ×1

difflib ×1

file ×1

generator ×1

md5 ×1

range ×1