如何在 GCP 中使用 Pandas 和云函数读取 csv 文件?

ala*_*lan 3 csv python-3.x pandas google-cloud-platform google-cloud-functions

我只是尝试读取上传到 GCS 的 csv 文件。

我想读取使用 GCP 中的云功能上传到 GCS 的 csv 文件。我想将 csv 数据作为“DataFrame”处理。

但是我无法使用 Pandas 读取 csv 文件。

这是使用云函数读取 GCS 上的 csv 文件的代码。

def read_csvfile(data, context):
     try:
          bucket_name = "my_bucket_name"
          file_name = "my_csvfile_name.csv"
          project_name = "my_project_name"

          # create gcs client
          client = gcs.Client(project_name)
          bucket = client.get_bucket(bucket_name)
          # create blob
          blob = gcs.Blob(file_name, bucket)
          content = blob.download_as_string()
          train = pd.read_csv(BytesIO(content))
          print(train.head())
     
     except Exception as e:
          print("error:{}".format(e))

Run Code Online (Sandbox Code Playgroud)

当我运行 Python 代码时,出现以下错误。

No columns to parse from file

一些网站说该错误意味着我读取了非空的 csv 文件。但实际上我上传了非空的csv 文件。那么我该如何解决这个问题呢?

请给我你的帮助。谢谢。

----添加于 2020/08/08-------

谢谢你给我的帮助!但最后我云没有使用你的代码读取 csv 文件......我仍然有错误,No columns to parse from file.

所以我尝试了将 csv 文件读取为 Byte 类型的新方法。读取 csv 文件的新 Python 代码如下。

主文件

from google.cloud import storage
import pandas as pd
import io
import csv
from io import BytesIO 

def check_columns(data, context):
    try:
        object_name = data['name']
        bucket_name = data['bucket']

        storage_client = storage.Client()
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(object_name)
        data = blob.download_as_string()
        
        #read the upload csv file as Byte type.
        f = io.StringIO(str(data))
        df = pd.read_csv(f, encoding = "shift-jis")

        print("df:{}".format(df))     
        print("df.columns:{}".format(df.columns)) 
        print("The number of columns:{}".format(len(df.columns)))
Run Code Online (Sandbox Code Playgroud)

要求.TXT

Click==7.0
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
Pillow==5.4.1
qrcode==6.1
six==1.12.0
Werkzeug==0.14.1
google-cloud-storage==1.30.0
gcsfs==0.6.2
pandas==1.1.0
Run Code Online (Sandbox Code Playgroud)

我得到的输出如下。

df:Empty DataFrame
Columns: [b'Apple, Lemon, Orange, Grape]
Index: []
df.columns:Index(['b'Apple', 'Lemon', 'Orange', 'Grape'])
The number of columns:4
Run Code Online (Sandbox Code Playgroud)

所以我只能将 csv 文件中的第一条记录读取为 df.column!?但是我无法在 csv 文件中获取其他记录......并且第一列不是列而是正常记录。

那么如何使用pandas将csv文件中的一些记录作为DataFrame获取

你能再帮我一次吗?谢谢你。

Jan*_*dez 8

从 0.24.1 版本开始,Pandas 可以直接读取 Google Cloud Storage URI。

例如:

gs://awesomefakebucket/my.csv

附加到函数的服务帐户必须有权读取 CSV 文件。

请随时测试和修改此代码。

我使用了 Python 3.7

函数.py

from google.cloud import storage
import pandas as pd

def hello_world(request):
    # it is mandatory initialize the storage client
    client = storage.Client()
    #please change the file's URI
    temp = pd.read_csv('gs://awesomefakebucket/my.csv', encoding='utf-8')
    print (temp.head())
    return f'check the results in the logs'

Run Code Online (Sandbox Code Playgroud)

要求.txt

google-cloud-storage==1.30.0
gcsfs==0.6.2
pandas==1.1.0

Run Code Online (Sandbox Code Playgroud)