我只想快速从 Google Cloud Datalab 笔记本中获取一些输出数据,最好是一次性 CSV 文件。

我已经这样做了:

writer = csv.writer(open('output.csv', 'wb'))
for row in rows:
    writer.writerow(row)

这会写入一个本地文件,但随后我无法在浏览器中打开它,或者(查看如何)从 Cloud Datalab 下载它。

如何以 CSV 文件的形式快速获取我的数据?我想也许我必须使用存储 API 并编写它?我发现文档有点难以理解,我有这样的事情:
import gcp
import gcp.storage as storage

// create CSV file? construct filepath? how?

mybucket = storage.Bucket(myfile)
mybucket.create()

最佳答案

至少有2个选项:

从 Datalab 本地下载文件

此选项在当前的 Datalab 代码中似乎不可用。我已为 Datalab 提交了一份 pull request,它可能会解决您的问题。该修复允许用户使用 Datalab 界面编辑/下载不是笔记本 (*.ipynb) 的文件。我能够使用拉取请求中的修改从 Datalab 下载/编辑文本文件。

将文件发送到 Google Cloud 中的存储桶

以下 link 可能有助于编写代码以使用 Storage API 将文件传输到 GCP 中的存储桶。

这是一个工作示例:

from datalab.context import Context
import datalab.storage as storage

sample_bucket_name = Context.default().project_id + '-datalab-example'
sample_bucket_path = 'gs://' + sample_bucket_name

sample_bucket = storage.Bucket(sample_bucket_name)

# Create storage bucket if it does not exist
if not sample_bucket.exists():
    sample_bucket.create()

# Write an item to the storage bucket
sample_item = sample_bucket.item('stringtofile.txt')
sample_item.write_to('This is a string', 'text/plain')

# Another way to copy an item from Datalab to Storage Bucket
!gsutil cp 'someotherfile.txt' sample_bucket_path

复制项目后,点击 here 以查看 Google Cloud 存储分区中的项目

关于google-cloud-datalab - 如何快速从 Google Cloud Datalab 笔记本中获取数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35719627/

10-12 21:04