我无法将数据帧保存到云存储

2024-09-30 16:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

def save_csv_to_cloud_storage(df,file_name,folder='output'):

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/Users/******/Desktop/*****.json'
    storage_client = storage.Client()
    bucket = storage_client.get_bucket('fluxlengow')
    now = datetime.now()
    dt_string = now.strftime("%Y%m%d-%H%M%S")
    f = StringIO()
    df.to_csv(f,sep=',')
    f.seek(0)
    Blob('{}/{}_{}_.csv'.format(folder, dt_string, file_name),bucket).upload_from_file(f,content_type='text/csv')


def lengowToStorage():

    liste = ['https://httpnas.****.*****/******/SUP****/*******_FR.csv','https://httpnas.****.*****/******/SUP****/*******_UK.csv','https://httpnas.****.*****/******/SUP****/*******_IT.csv']
    for i in liste :
        name = i.split('/')[-1]
        name = name.split('.')[0]
        CSV_URL = '{}'.format(i)
        with requests.Session() as s:
            download = s.get(CSV_URL)
            decoded_content = download.content.decode('utf8')
            cr = csv.reader(decoded_content.splitlines(), delimiter='|')
            my_list = list(cr)
            df_ = pd.DataFrame(my_list, columns=my_list[0]).drop(0)
            save_csv_to_cloud_storage(df_,file_name=name,folder='input')
            print("recuperation du fichier : {}".format(i))

lengowToStorage()

嗨,伙计们,我很抱歉,但我需要你们的帮助,因为我真的在这个编码问题上卡住了。 我正在尝试将数据帧作为CSV文件发送到云存储。 不幸的是,当我试图将它保存到存储器时,我遇到了这个错误

'latin-1' codec can't encode character '\u2019' in position 32318: Body ('’') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

然后,我对每一列进行了“utf-8”编码(代码中未显示),保存到云存储确实有效,但我的数据无效 这些文件的格式如下:

b'Antaeus Lotion Apr\xc3\xa8s Rasage 100ml'

我解码UTF8,我编码UTF8。。。 但无法将我的数据转换为我想要的字符串版本

'Antaeus Lotion Après Rasage 100ml'

如果你能帮忙,我将非常感激


Tags: csvtonamehttpsformatdfbucketstorage