使用 node 和 python 读取相同文件时的结果差异

Difference in result while reading same file with node and python

我一直在尝试使用 Python 在 Hyperledger Fabric 中读取 this file of the Node SDK 中给出的 genesis.block 的内容。但是,每当我尝试使用

读取带有 Python 的文件时
data = open("twoorgs.genesis.block").read()

data变量的值如下:

>>> data
'\n'

对于使用 fs.readFileSync() 的 nodejs,我获得了同一文件的 Buffer() 实例。

var data = fs.readFileSync('./twoorgs.genesis.block');

结果是

> data
<Buffer 0a 22 1a 20 49 63 63 ac 9c 9f 3e 48 2c 2c 6b 48 2b 1f 8b 18 6f a9 db ac 45 07 29 ee c0 bf ac 34 99 9e c2 56 12 e1 84 01 0a dd 84 01 0a d9 84 01 0a 79 ... >

如何使用 Python 成功读取此文件?

您的文件中有 1a。这是 Ctrl-Z,这是 Windows 上的文件结尾。

所以尝试像这样的二进制模式:

data = open("twoorgs.genesis.block", 'rb').read()