【问题标题】:Read a binary file stored in HDFS with Python使用 Python 读取存储在 HDFS 中的二进制文件
【发布时间】:2021-09-01 07:22:48
【问题描述】:

我有一些二进制文件。 当我将它们存储到本地文件时,我可以将它们作为二进制文件读取。

with open("binary_file", 'rb') as f:
    print("Binary file:  ", f.read())

结果:

Binary file:   b'Ix\x9d\xdf\xd2\xf6\x83\xe8B\x95.... (a long binary)

但我想存储它们并从 HDFS 检索它们。当我使用以下命令时:

f = os.popen("hdfs dfs -cat binary_file")
print("Binary file:  ", f.read())

“打印”出现错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 2: invalid start byte

我认为这个命令将文件作为文本文件读取。如何将文件显式读取为二进制文件?

【问题讨论】:

    标签: python hdfs binaryfiles python-cryptography


    【解决方案1】:

    os.popen() 默认为文本r 模式。

    请改用subprocess.check_output();它默认为二进制:

    import subprocess
    output = subprocess.check_output("hdfs dfs -cat encrypted_file", shell=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多