如何从Google云端存储读取前两行的CSV [英] How to read first 2 rows of csv from Google Cloud Storage

查看:58
本文介绍了如何从Google云端存储读取前两行的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取csv文件的前2行并将其存储在数组中,以确定file_type,reportingdate和其他元数据。我的文件在Google云存储中。如何读取前两行并将其存储在python脚本数组中。我正在尝试以下代码:

I need to read first 2 rows of csv file and store that in array to determine file_type,reportingdate and other metadata. My file is in Google cloud Storage.How can I read first 2 rows and store that in array in python script . I am trying with below code :

storage_client = storage.Client()

from google.cloud import storage
bucket = storage_client.get_bucket('test_deepshikha')
blob = storage.Blob('intercompany_2rows_duplicates.csv', bucket)
content = blob.download_as_string()

for row in content:
    print (row)

正在打印csv数据,但每个字符都作为字符串打印。我需要数组中的行数据。任何帮助将不胜感激。

It is printing the csv data but every character is being printed as string. I need row data in array. Any help will be much appreciated.

推荐答案

由于函数名,我假设content变量是一个很长的字符串。如果真是这样,那么字符串中应该有换行符。您可以简单地使用换行符将字符串拆分为定界符,并获取前两个值。像这样的东西:

Due to the function name, I am assuming that the content variable is one very long string. If that is the case, then there ought to be newline characters in the string. You can simply split the string using newline as a delimiter and grab the first two values. Something like this:

lines = content.split('\n')[:2]

这篇关于如何从Google云端存储读取前两行的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆