[英]How to read first 2 rows of csv from Google Cloud Storage
我需要读取csv文件的前2行并将其存储在数组中,以确定file_type,reportingdate和其他元数据。 我的文件在Google云存储中。如何读取前两行并将其存储在python脚本数组中。 我正在尝试以下代码:
storage_client = storage.Client()
from google.cloud import storage
bucket = storage_client.get_bucket('glrs_test_deepshikha')
blob = storage.Blob('67_glrs_intercompany_override_2rows_duplicates.csv', bucket)
content = blob.download_as_string()
for row in content:
print (row)
它正在打印csv数据,但每个字符都作为字符串打印。 我需要数组中的行数据。 任何帮助都感激不尽。
由于函数名,我假设content变量是一个很长的字符串。 如果是这种情况,则字符串中应该有换行符。 您可以简单地使用换行符将字符串拆分为定界符,并获取前两个值。 像这样:
lines = content.split('\n')[:2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.