如何打开存储在 Google App Engine 中的二进制文件?

How to open a binary file stored in Google App Engine?

我已经使用 word2vec 生成了一个二进制文件,将生成的 .bin 文件存储到我的 GCS 存储桶中,并且 运行 我的 App Engine 应用程序处理程序中的以下代码:

    gcs_file = gcs.open(filename, 'r')
    content = gcs_file.read().encode("utf-8")
    """ call word2vec with content so it doesn't need to read a file itself, as we don't have a filesystem in GAE """

失败并出现此错误: content = gcs_file.read().encode("utf-8") UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 15: ordinal not in range(128)

如果我尝试 gcs_file.read()gcs_file.read().decode("utf-8").encode("utf-8").

,则会发生类似的解码错误

关于如何从 GCS 读取二进制文件有什么想法吗?

谢谢

如果它是二进制那么它不会采用字符编码,这就是 UTF-8 是什么。 UTF-8 只是字符集(String 数据)Unicode 规范的一种可能的二进制编码。您需要返回并阅读 UTF-8ASCII 代表的内容以及它们的使用方式。

如果不是使用特定编码编码的文本数据,那么它不会神奇地只是 decode,这就是您收到该错误的原因。 can't decode byte 0xf6 in position 15 不是有效的 ASCII 值。