gpt4 book ai didi

python - 如何创建新的 CSV 文件并将其存储在 Google Cloud Storage 中?

转载 作者:行者123 更新时间:2023-12-01 00:56:43 25 4
gpt4 key购买 nike

我有这个代码:

import csv
from google.cloud import storage
import os

os.environ["Credientials"]
storage_client = storage.Client()

mylist = ['Row1','Row2','Row3'] # Rows I'm making for CSV file

with open('Afile.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) # Making CSV file
wr.writerow(mylist)

s = myfile

def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))

upload_blob('BucketName', s ,'storage location') # Uploading the newly made CSV file to Google Cloud Storage

制作新的 CSV 文件(由 s 引用),并且我尝试将新制作的文件上传到 Google Cloud 存储,而不是从本地文件路径上传。当我运行此程序时,出现以下错误:

TypeError: expected string or bytes-like object

我怎样才能让它工作,以便我可以制作一个新制作的 CSV 文件,然后将其上传到 Google Cloud Storage?如果这可能的话?

感谢您的帮助!

最佳答案

with open('Afile.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) # Making CSV file
wr.writerow(mylist)

s = myfile

这里的s是一个类似文件的对象而不是字符串或字节对象。您实质上是传递文件本身而不是文件名。您可能应该将 Afile.csv 作为字符串传递。

关于python - 如何创建新的 CSV 文件并将其存储在 Google Cloud Storage 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56191952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com