<bdo id='nGB0Z'></bdo><ul id='nGB0Z'></ul>
  1. <legend id='nGB0Z'><style id='nGB0Z'><dir id='nGB0Z'><q id='nGB0Z'></q></dir></style></legend>
  2. <small id='nGB0Z'></small><noframes id='nGB0Z'>

    <i id='nGB0Z'><tr id='nGB0Z'><dt id='nGB0Z'><q id='nGB0Z'><span id='nGB0Z'><b id='nGB0Z'><form id='nGB0Z'><ins id='nGB0Z'></ins><ul id='nGB0Z'></ul><sub id='nGB0Z'></sub></form><legend id='nGB0Z'></legend><bdo id='nGB0Z'><pre id='nGB0Z'><center id='nGB0Z'></center></pre></bdo></b><th id='nGB0Z'></th></span></q></dt></tr></i><div id='nGB0Z'><tfoot id='nGB0Z'></tfoot><dl id='nGB0Z'><fieldset id='nGB0Z'></fieldset></dl></div>

    <tfoot id='nGB0Z'></tfoot>

      将 pandas 数据帧从本地机器上传到Google Cloud Bucket

      时间:2024-04-20
        <bdo id='I9yAE'></bdo><ul id='I9yAE'></ul>

            <tfoot id='I9yAE'></tfoot>

            <legend id='I9yAE'><style id='I9yAE'><dir id='I9yAE'><q id='I9yAE'></q></dir></style></legend>

            <small id='I9yAE'></small><noframes id='I9yAE'>

                <i id='I9yAE'><tr id='I9yAE'><dt id='I9yAE'><q id='I9yAE'><span id='I9yAE'><b id='I9yAE'><form id='I9yAE'><ins id='I9yAE'></ins><ul id='I9yAE'></ul><sub id='I9yAE'></sub></form><legend id='I9yAE'></legend><bdo id='I9yAE'><pre id='I9yAE'><center id='I9yAE'></center></pre></bdo></b><th id='I9yAE'></th></span></q></dt></tr></i><div id='I9yAE'><tfoot id='I9yAE'></tfoot><dl id='I9yAE'><fieldset id='I9yAE'></fieldset></dl></div>
                  <tbody id='I9yAE'></tbody>
              1. 本文介绍了将 pandas 数据帧从本地机器上传到Google Cloud Bucket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想直接从本地机器上传 pandas 数据框到Google云存储,所以我不在云函数中。我使用write-a-pandas-dataframe-to-google-cloud-storage-or-bigquery尝试了不同的方法。但我无法保存。

                注意:我只能使用google.cloud包

                下面是我尝试的代码

                from google.cloud import storage
                import pandas as pd
                input_dict = [{'Name': 'A', 'Id': 100}, {'Name': 'B', 'Id': 110}, {'Name': 'C', 'Id': 120}]
                df = pd.DataFrame(input_dict)
                

                尝试:1

                destination = f'gs://bucket_name/test.csv'
                df.to_csv(destination)
                

                尝试:2

                storage_client = storage.Client(project='project')
                bucket = storage_client.get_bucket('bucket_name')
                gs_file = bucket.blob('test.csv')
                df.to_csv(gs_file)
                

                我正在显示以下错误

                对于选项1:没有这样的文件或目录:‘gs://bucket_name/test.csv’

                选项2:‘blob’对象没有‘Close’属性

                谢谢,

                Raghunath。

                推荐答案

                from google.cloud import storage
                import os
                from io import StringIO # if going with no saving csv file
                
                # say where your private key to google cloud exists
                os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/your-google-cloud-private-key.json'
                
                df = pd.DataFrame([{'Name': 'A', 'Id': 100}, {'Name': 'B', 'Id': 110}])
                

                先将其写入您计算机上的CSV文件,然后上传:

                df.to_csv('local_file.csv')
                gcs.get_bucket('BUCKET_NAME').blob('FILE_NAME.csv').upload_from_filename('local_file.csv', content_type='text/csv')
                

                如果不想创建临时CSV文件,请使用StringIO:

                f = StringIO()
                df.to_csv(f)
                f.seek(0)
                gcs.get_bucket('BUCKET_NAME').blob('FILE_NAME.csv').upload_from_file(f, content_type='text/csv')
                

                这篇关于将 pandas 数据帧从本地机器上传到Google Cloud Bucket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:带有实例方法的Python函数工具lru_cache:Release对象 下一篇:如何将文件从Google云存储打开到云函数

                相关文章

                1. <i id='PTPSK'><tr id='PTPSK'><dt id='PTPSK'><q id='PTPSK'><span id='PTPSK'><b id='PTPSK'><form id='PTPSK'><ins id='PTPSK'></ins><ul id='PTPSK'></ul><sub id='PTPSK'></sub></form><legend id='PTPSK'></legend><bdo id='PTPSK'><pre id='PTPSK'><center id='PTPSK'></center></pre></bdo></b><th id='PTPSK'></th></span></q></dt></tr></i><div id='PTPSK'><tfoot id='PTPSK'></tfoot><dl id='PTPSK'><fieldset id='PTPSK'></fieldset></dl></div>
                  <tfoot id='PTPSK'></tfoot>

                  <small id='PTPSK'></small><noframes id='PTPSK'>

                2. <legend id='PTPSK'><style id='PTPSK'><dir id='PTPSK'><q id='PTPSK'></q></dir></style></legend>

                      <bdo id='PTPSK'></bdo><ul id='PTPSK'></ul>