Google cloud platform 如何在Google云存储上写入XLSX文件
如何使用python在Google云存储上写入xlsx文件?我现在正在执行此操作,但不确定如何格式化write()以添加到行中。我想在云存储上的my names.xlsx文件中添加一行['Sally','Ja',15]Google cloud platform 如何在Google云存储上写入XLSX文件,google-cloud-platform,google-cloud-storage,Google Cloud Platform,Google Cloud Storage,如何使用python在Google云存储上写入xlsx文件?我现在正在执行此操作,但不确定如何格式化write()以添加到行中。我想在云存储上的my names.xlsx文件中添加一行['Sally','Ja',15] import cloudstorage file = cloudstorage.open('/master/names.xlsx') file.write(##what goes in here?##) filehandle.close() Google云存储中的对象是不可
import cloudstorage
file = cloudstorage.open('/master/names.xlsx')
file.write(##what goes in here?##)
filehandle.close()
Google云存储中的对象是不可变的,因此不能将新行附加到现有对象。您需要重写整个对象
使用可以获得有限的“追加”功能,但这只会将字节追加到对象的结尾,并将字节追加到对象的结尾,我认为对于XLSX数据来说,这不容易实现。谷歌云存储中的对象是不可变的,因此无法将新行追加到现有对象。您需要重写整个对象
使用可以获得有限的“追加”功能,但这只会将字节追加到对象的末尾,并将字节追加到对象的末尾,我认为对于XLSX数据来说,这不容易实现。正如Travis所提到的,您不能追加,只能重新重写整个对象,下面的示例(假设text.csv是您现有的文件),您可以在dataframe中读取该文件,添加一些数据并使用gsutil命令将其复制到GCP bucket。这将覆盖先前版本的text.csv
import pandas as pd
data = [['Alex','Feb',10],['Bob','jan',12]]
df = pd.DataFrame(data,columns=['Name','Month','Age'])
print df
输出
Name Month Age
0 Alex Feb 10
1 Bob jan 12
Name Month Age
0 Alex Feb 10
1 Bob jan 12
2 Sally Oct 15
添加一行
row = ['Sally','Oct',15]
df.loc[len(df)] = row
print df
输出
Name Month Age
0 Alex Feb 10
1 Bob jan 12
Name Month Age
0 Alex Feb 10
1 Bob jan 12
2 Sally Oct 15
使用gsutil写入/复制GCP存储桶
df.to_csv('text.csv', index = False)
!gsutil cp 'text.csv' 'gs://BucketName/folderName/'
使用python写入/复制到GCP存储桶
`pip3 install xlsxwriter # install package`
python代码
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)
正如Travis所提到的,您不能追加而是重新重写整个对象,下面的示例(假设text.csv是您现有的文件),您可以在dataframe中读取文件,添加一些数据,并使用gsutil命令将其复制到GCP bucket。这将覆盖先前版本的text.csv
import pandas as pd
data = [['Alex','Feb',10],['Bob','jan',12]]
df = pd.DataFrame(data,columns=['Name','Month','Age'])
print df
输出
Name Month Age
0 Alex Feb 10
1 Bob jan 12
Name Month Age
0 Alex Feb 10
1 Bob jan 12
2 Sally Oct 15
添加一行
row = ['Sally','Oct',15]
df.loc[len(df)] = row
print df
输出
Name Month Age
0 Alex Feb 10
1 Bob jan 12
Name Month Age
0 Alex Feb 10
1 Bob jan 12
2 Sally Oct 15
使用gsutil写入/复制GCP存储桶
df.to_csv('text.csv', index = False)
!gsutil cp 'text.csv' 'gs://BucketName/folderName/'
使用python写入/复制到GCP存储桶
`pip3 install xlsxwriter # install package`
python代码
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)
但我想让我的谷歌应用程序引擎应用程序创建一个xlsx文件并存储在谷歌存储中work@John用python代码更新了我的帖子以访问GCS bucket。@eric chen,请参阅我的帖子,但我希望我的google app engine应用程序创建一个xlsx文件并存储在google存储中。此方法不会work@JohnU用python代码更新我的帖子以访问GCS bucket。@eric chen,请参阅我的帖子