解决 python3.X TypeError: write() argument must be str, not bytes

本文介绍如何在Python中正确地将模型保存为文件。重点讲解了在不同Python版本中,使用pickle模块保存模型时应选择正确的文件打开模式。通过示例说明了在Python 3中保存模型时需要使用二进制模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import pickle as p

output_model_file = 'saved_model.pkl'
with open(output_model_file,'w') as f:
    p.dump(linear_regressor,f)
TypeError: write() argument must be str, not bytesp

In Python 3 it makes a difference whether you open the file in binary or text mode. Just add the bflag to make it binary:

with open('random.bin','wb') as f:

This works in Python 2 too.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值