How to write to XLSX file on Google Cloud Storage

前端 未结 2 830
独厮守ぢ
独厮守ぢ 2020-12-22 12:37

How can I write to an xlsx file on Google Cloud Storage using python? I\'m doing this right now but unsure how to format my write() to add in a row. I want to add in a row [

2条回答
  •  时光说笑
    2020-12-22 12:53

    As mentioned by Travis , you cannot append but re-rewrite the entire object ,example below(assuming text.csv is your existing file) , you can read the file in dataframe , add some data and copy it using gsutil command to GCP bucket. This will overwrite the previous version of text.csv.

      import pandas as pd
      data = [['Alex','Feb',10],['Bob','jan',12]]
      df = pd.DataFrame(data,columns=['Name','Month','Age'])
      print df
    

    Output

           Name Month  Age
        0  Alex   Feb   10
        1   Bob   jan   12
    

    Add a row

      row = ['Sally','Oct',15]
      df.loc[len(df)] = row
      print df
    

    output

        Name Month  Age
    0   Alex   Feb   10
    1    Bob   jan   12
    2  Sally   Oct   15
    

    write/copy to GCP Bucket using gsutil

     df.to_csv('text.csv', index = False)
    !gsutil cp 'text.csv' 'gs://BucketName/folderName/'
    

    write/copy to GCP Bucket using python

    `pip3 install xlsxwriter # install package`
    

    python code

    from google.cloud import storage
    import pandas as pd
    
    #define configurations
    bucket_name = 'my_gcp_bucket'
    xlsx_file = 'output.xlsx'
    
    #create dataframe
    data = [['Alex','Feb',10],['Bob','jan',12]]
    df = pd.DataFrame(data,columns=['Name','Month','Age'])
    df.to_excel("output.xlsx")
    
    #Upload to Google cloud storage
    client = storage.Client()
    bucket = client.get_bucket(bucket_name)
    blob = bucket.blob(xlsx_file)
    blob.upload_from_filename(xlsx_file)
    

提交回复
热议问题