Storage中的文本文件直接读入python

Storage中的文本文件直接读入python

本文介绍了GCS-将Google Cloud Storage中的文本文件直接读入python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有点愚蠢.我已经阅读了许多文档和stackoverflow问题,但我做对了.

I feel kind of stupid right now. I have been reading numerous documentations and stackoverflow questions but I can't get it right.

我在Google Cloud Storage上有一个文件.它在存储桶"test_bucket"中.在此存储桶中,有一个文件夹"temp_files_folder",其中包含两个文件,一个名为"test.txt"的.txt文件和一个名为"test.csv"的.csv文件.这两个文件只是因为我尝试同时使用这两个文件,但是两种方法的结果都是相同的.

I have a file on Google Cloud Storage. It is in a bucket 'test_bucket'. Inside this bucket there is a folder, 'temp_files_folder', which contains two files, one .txt file named 'test.txt' and one .csv file named 'test.csv'. The two files are simply because I try using both but the result is the same either way.

文件中的内容是

hej
san

并且我希望将其读入python,就像使用

and I am hoping to read it into python the same way I would do on a local with

textfile = open("/file_path/test.txt", 'r')
times = textfile.read().splitlines()
textfile.close()
print(times)

给出

['hej', 'san']

我尝试使用

from google.cloud import storage

client = storage.Client()

bucket = client.get_bucket('test_bucket')

blob = bucket.get_blob('temp_files_folder/test.txt')

print(blob.download_as_string)

但是它给出了输出

<bound method Blob.download_as_string of <Blob: test_bucket, temp_files_folder/test.txt>>

如何获取文件中的实际字符串?

How can I get the actual string(s) in the file?

推荐答案

download_as_string是一种方法,您需要对其进行调用.

download_as_string is a method, you need to call it.

print(blob.download_as_string())

更有可能,您希望将其分配给变量,以便您下载一次,然后可以打印它并对其进行任何其他操作:

More likely, you want to assign it to a variable so that you download it once and can then print it and do whatever else you want with it:

downloaded_blob = blob.download_as_string()
print(downloaded_blob)
do_something_else(downloaded_blob)

这篇关于GCS-将Google Cloud Storage中的文本文件直接读入python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:59