Google Cloud Datalab错误写入云端存储

时间:2018-03-06 03:01:48

标签: google-cloud-storage google-cloud-datalab

我第一次使用Google Cloud Datalab为Kaggle比赛构建分类器。但我很难尝试使用google.datalab.storage API将包含预处理培训数据的csv文件写入云存储。

该文件包含带有unicode字符的字符串,这会导致Storage对象的write_stream触发错误: 无法处理HTTP响应。

以下是仅尝试编写单个字符串的简化代码:

from google.datalab import Context
import google.datalab.storage as storage

project = Context.default().project_id
bucket_name = project
bucket_object = storage.Bucket(bucket_name)
file_object = bucket_object.object('x.txt')

test_string = 'Congratulations from me as well, use the tools well. \xc2\xa0\xc2\xb7 talk'
#test_string = 'Congratulations from me as well, use the tools well.  talk'

print type(test_string)
print len(test_string)
test_string = test_string.decode('utf-8')
print type(test_string)
print len(test_string)
test_string = test_string.encode('utf-8')
print type(test_string)
print len(test_string)

try:
  file_object.write_stream(test_string, 'text/plain')
except Exception as e:
  print e

输出:

<type 'str'>
62
<type 'unicode'>
60
<type 'str'>
62
Failed to process HTTP response.

如果我使用不带Unicode字符的字符串,则会创建Storage对象并将字符串写入文件。我是否尝试编写Unicode解码版本或编码的字符串没有区别。内容类型('text / plain'或'application / octet-stream')也没有区别。

我很感激任何帮助或想法如何解决这个问题,特别是因为google.datalab.storage API几乎没有记录(像大多数GCP一样)。

THX。

0 个答案:

没有答案