问题描述
我现在觉得有点傻.我一直在阅读大量文档和 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
我希望像在本地使用
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!