如何将写入列表数据的csv文件上传到GCP

时间:2020-08-25 05:54:25

标签: python-3.x csv google-cloud-platform io google-cloud-functions

我想使用云功能将写入列表数据的csv文件上传到GCS。 我想上传csv文件作为字符代码UTF-8。

实际上,我可以上传写入了字符串数据的csv文件,但不能上传写入了 list 数据的csv文件。

以下代码用于将字符串数据写入csv文件,我可以将其上传到GCS

【MAIN.PY】

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

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_file(source_file_name)

def upload_csvfile(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)

        message = "Data for CSV file"  
        with open('/tmp/test.csv', "w", encoding='cp932') as f: 
            f.write(message) 
        with open('/tmp/test.csv', 'r', encoding='utf8') as file_obj: 
            upload_blob('test_bucket', file_obj, 'csv_test1.csv')
    except Exception as e:
          print(e)

【REQUIREMENTS.TXT】

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

下一个下面的代码是将 list 数据写入csv文件中,而我无法上传。

from google.cloud import storage
import csv
import codecs
from io import BytesIO 
from io import StringIO

#write list into csv file
def export_list_csv(export_list, csv_dir):
    with open(csv_dir, "w", encoding='cp932') as f:
        writer = csv.writer(f, lineterminator='\n')
        if isinstance(export_list[0], list): #in case of multidimension
            writer.writerows(export_list)
        else:
            writer.writerow(export_list) # in case of single dimension

def upload_csvfile(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)

          #message = [1,2,3, "apple", "りんご", "林檎"] # single dimension
          message = [[1,2,3], ["apple", "りんご", "林檎"]] # multidimension 
          with open('/tmp/test.csv', "w", encoding='cp932') as f: 
               writer = csv.writer(f)
               if isinstance(message[0], list): # for multidimension 
                    writer.writerows(message)
               else:                            # for single dimension
                    writer.writerow(message)
          with codecs.open("/tmp/test.csv", "r", "utf-8", "ignore") as file_obj: 
               print("file_obj.read():{0}".format(file_obj.read())) 

               #upload csv file as 'csv_test1.csv' to 'test_bucket'
               storage_client = storage.Client()
               bucket4 = storage_client.get_bucket('test_bucket')
               blob4 = bucket4.blob('csv_test1.csv')
               blob4.upload_from_file(file_obj)

     except Exception as e:
          print("error:{}".format(e))

我得到了这个错误。

Stream must be at beginning

然后我修复波纹管之类的代码。

            #message = [1,2,3, "apple", "りんご", "林檎"] # single dimension
            message = [[1,2,3], ["apple", "りんご", "林檎"]] # multidimension 
            with open('/tmp/test.csv', "w", encoding='cp932') as f: 
                writer = csv.writer(f)
                if isinstance(message[0], list): # for multidimension 
                    writer.writerows(message)
                else:                            # for single dimension
                    writer.writerow(message)

            with codecs.open("/tmp/test.csv", "r", "utf-8", "ignore") as file_obj:
                print("file_obj.read():{0}".format(file_obj.read())) 
                out_file = StringIO(file_obj.read())  # 1/2 I fix here

                #upload csv file as 'csv_test1.csv' to 'test_bucket'
                storage_client = storage.Client()
                bucket4 = storage_client.get_bucket('test_bucket')
                blob4 = bucket4.blob('csv_test1.csv')
                blob4.upload_from_file(out_file)    # 2/2 I fix here

实际上,我可以将csv文件上传到GCS,但是csv文件本身没有任何数据。 csv文件应具有列表数据...

我不知道要解决此问题并在GCS中上传CSV文件。

请有人帮我。谢谢。

0 个答案:

没有答案