GCS - 从 Google Cloud Storage 直接读取文本文件到 python

2023-11-07Python开发问题
0

本文介绍了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

我希望像在本地使用

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10