【问题标题】:How to open a binary file stored in Google App Engine?如何打开存储在 Google App Engine 中的二进制文件?
【发布时间】:2016-05-17 21:28:48
【问题描述】:

我已使用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 读取二进制文件的任何想法?

谢谢

【问题讨论】:

  • 为什么要调用encode?如果是字节,则已经编码。
  • 不是你不知道的字节知道它是什么,如果它是png那么它肯定不是已经使用编码一个字符编码。除非它被转换为Base64UUE,但我很确定从代码和问题中也不是的情况。

标签: python google-app-engine binary google-cloud-storage word2vec


【解决方案1】:

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

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

【讨论】:

  • 很公平。让我感到困惑的是,我从您的回答中学到的教训是二进制文件已经编码。谢谢。
猜你喜欢
  • 2019-06-02
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多