我使用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读取二进制文件的任何想法?
由于
答案 0 :(得分:0)
如果它是二进制的,则它不会采用字符编码,这就是UTF-8
。 UTF-8
只是字符集(Unicode
数据)的String
规范的一种可能的二进制编码。您需要返回并阅读UTF-8
和ASCII
代表的内容及其使用方式。
如果不是使用特定编码编码的文本数据,那么它就不会奇怪地只是decode
,这就是你得到错误的原因。 can't decode byte 0xf6 in position 15
不是有效的ASCII
值。