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

我正在尝试使用以下代码行将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放空值?过去我对此没有任何问题,我不记得自己做过什么。

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

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

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,content_type='application/octet-stream')

输出文件将是:

col1,0.0

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

 content_type=''application/octet-stream' 

这是语法错误

  

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

本文链接:https://www.f2er.com/3145356.html

大家都在问