How to read first 2 rows of csv from Google Cloud

2019-08-19 00:23发布

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('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)

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.

1条回答
够拽才男人
2楼-- · 2019-08-19 00:48

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]
查看更多
登录 后发表回答