bucket.blob.upload_from_string返回“ ValueError:无法将任何内容都转换为unicode”

时间:2019-11-07 11:15:37

标签: python google-cloud-platform google-cloud-storage

我正在尝试使用以下代码行将DataFrame(df)上传到云存储中:

bucket.blob(gcs_reference).upload_from_string(df.to_csv(index=False, encoding='utf-8'), content_type=''application/octet-stream')

但是,我的DataFrame对于某些单元格没有值,因此会触发错误:

  

ValueError:无法将任何内容都不转换为Unicode

是否有办法抑制此情况,并为None放空值?过去我对此没有任何问题,我不记得自己做过什么。

1 个答案:

答案 0 :(得分:1)

我能够复制,并且对我有用:

import pandas as pd
from gcloud import storage
client = storage.Client()

bucket = client.get_bucket('source')
d = {'col1': [1, 2], 'col2': [3, None]}
df = pd.DataFrame(data=d)

bucket.blob('file').upload_from_string(df.to_csv(index=False, encoding='utf-8'), 
content_type='application/octet-stream')

文件内容为:

col1,col2
1,3.0
2,

或者您可以使用:

df.fillna(value=0, inplace=True)
bucket.blob('file').upload_from_string(df.to_csv(index=False, 
encoding='utf-8'), content_type='application/octet-stream')

输出文件将是:

col1,col2
1,3.0
2,0.0

您的代码中包含以下部分:

 content_type=''application/octet-stream' 

这是语法错误

  

content_type =''应用程序/八位字节流''