我只是想快速从笔记本中获取一些输出数据,最好是作为一个丢弃的CSV文件。
我做过这件事
writer = csv.writer(open('output.csv', 'wb'))
for row in rows:
writer.writerow(row)
这会写入一个本地文件,但是我既不能在浏览器中打开它,也不能从Cloud下载它。
如何快速抓取作为CSV文件的数据?我想也许我必须使用Storage并编写它?我发现这些文档有点难以理解,我有这样的发现:
import gcp
import gcp.storage as storage
// create CSV file? construct filepath? how?
mybucket = storage.Bucket(myfile)
mybucket.create()
发布于 2016-03-05 11:20:59
至少有两种选择:
从Datalab本地下载文件
此选项在当前Datalab代码中似乎不可用。我已经为Datalab提交了一个拉请求,这可能解决您的问题。修复允许用户使用Datalab接口编辑/下载非笔记本(*.ipynb)的文件。我能够从Datalab下载/编辑一个文本文件,使用在拉请求中的修改。
将文件发送到Google 中的存储桶
以下链接可能有助于编写代码,使用storage将文件传输到Google中的存储桶中。
下面是一个有用的例子:
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
复制项目后,单击这里在Google中的存储桶中查看该项
发布于 2016-03-01 16:17:47
你在说多少数据?我假设这不是一个BigQuery表,因为我们有相应的API。
对于存储API,可以将存储桶看作是一个文件夹。你需要在桶里创建一个项目。如果将数据作为字符串分配给Python变量,则可以使用API (write_to)。
如果像对output.csv那样写入文件,则该文件驻留在Datalab正在运行的Docker容器中。这意味着它是短暂的,当容器被关闭时会消失。但是,在此期间它是可访问的,您可以使用%bash单元格魔术将其发送到其他目的地,例如使用curl。
发布于 2017-03-09 20:54:50
我找到了一种从datalab笔记本编写csv文件的更简单的方法。
%storage write --object "gs://pathtodata/data.csv" --variable data
这里‘数据’是一个数据在你的笔记本!
https://stackoverflow.com/questions/35719627
复制