在下面,我假设您有python3.x(特别是3.5)。
让我们分析一下文档以了解函数:difflib.diff_bytes(dfunc, a, b, fromfile=b'', tofile=b'', fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n')
Compare a and b
(lists of bytes objects) using dfunc; yield a sequence of delta lines
(also bytes) in the format returned by dfunc. dfunc must be a
callable, typically either unified_diff() or context_diff().
Allows you to compare data with unknown or inconsistent encoding. All
inputs except n must be bytes objects, not str. Works by losslessly
converting all inputs (except n) to str, and calling dfunc(a, b,
fromfile, tofile, fromfiledate, tofiledate, n, lineterm). The output
of dfunc is then converted back to bytes, so the delta lines that you
receive have the same unknown/inconsistent encodings as a and b.
首先要注意的是字节对象和str(ing)对象之间的区别。那么除了n之外的每个输入参数都必须字节对象。在
所以关键是使用这个函数并向它传递字节对象,而不是字符串。因此,如果您有一个字符串,您应该在Python中使用b前缀,这将生成bytes类型的实例,而不是str(ing)类型的实例。
我建议你阅读
What does the 'b' character do in front of a string literal?
string_literals
因此,我将不再进一步解释这一部分。
因为我发现difflib.diff_bytes的文档有点神秘,所以我决定直接查看CPython本身用来测试该函数的代码。
这是一个很好的练习,有助于理解如何使用此功能。
测试difflib.diff_bytes的代码位于
中(假设您使用的是python3.5)
test_difflib
让我们检查该文件中的一个示例以了解发生了什么。在def test_byte_content(self):
# if we receive byte strings, we return byte strings
a = [b'hello', b'andr\xe9'] # iso-8859-1 bytes
b = [b'hello', b'andr\xc3\xa9'] # utf-8 bytes
unified = difflib.unified_diff
context = difflib.context_diff
check = self.check
check(difflib.diff_bytes(unified, a, a))
check(difflib.diff_bytes(unified, a, b))
# now with filenames (content and filenames are all bytes!)
check(difflib.diff_bytes(unified, a, a, b'a', b'a'))
check(difflib.diff_bytes(unified, a, b, b'a', b'b'))
# and with filenames and dates
check(difflib.diff_bytes(unified, a, a, b'a', b'a', b'2005', b'2013'))
check(difflib.diff_bytes(unified, a, b, b'a', b'b', b'2005', b'2013'))
# same all over again, with context diff
check(difflib.diff_bytes(context, a, a))
check(difflib.diff_bytes(context, a, b))
check(difflib.diff_bytes(context, a, a, b'a', b'a'))
check(difflib.diff_bytes(context, a, b, b'a', b'b'))
check(difflib.diff_bytes(context, a, a, b'a', b'a', b'2005', b'2013'))
check(difflib.diff_bytes(context, a, b, b'a', b'b', b'2005', b'2013'))
如您所见,a和b是包含每个文件内容的列表。然后程序定义两个变量,它们表示函数的dfunc参数。还要注意“b”前缀。difflib.diff_bytes将以字节对象的形式返回增量线。然后您必须编写自己的函数来检查。
另一个包含在diff文件中的文件名也包含在该文件中:
^{pr2}$
现在可以看到,文件名作为字节对象包含在增量行中。在