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

时间:2016-05-17 21:28:49

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

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

由于

1 个答案:

答案 0 :(得分:0)

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

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