TypeError: encoding or errors without a string argument

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-1 18:44   11   0

TypeError: encoding or errors without a string argument:没有一个字符串参数编码或错误(百度翻译),

import base64
import io
a = "<urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>"
b = base64.b64encode(bytes(a,'gb2312')) # 对字符串编码  URLError(ConnectionRefusedError(10061, '由于目标计算机积极拒绝,无法连接。', None, 10061),) this is a test   <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>
print ('b11:',b)
print ('b12:',base64.b64decode(b) )# 对字符串解码
c = io.StringIO()
c.write(a)
d = io.StringIO()
e = io.StringIO()
c.seek(0)
print(c, d)
#s1=str(c)
base64.b64encode(bytes(c,'gb2312'))# 对StringIO内的数据进行编码

运行报错:TypeError: encoding or errors without a string argument ,(发生于此行代码base64.b64encode(bytes(c,'gb2312'))

原因:用print(type(C))可看到c 类型为<class '_io.StringIO'>,因此需要对c进行str转换:

 
import base64
import io
a = "<urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>"
b = base64.b64encode(bytes(a,'gb2312')) # 对字符串编码  URLError(ConnectionRefusedError(10061, '由于目标计算机积极拒绝,无法连接。', None, 10061),) this is a test   <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>
print ('b11:',b)
print ('b12:',base64.b64decode(b) )# 对字符串解码
c = io.StringIO()
c.write(a)
d = io.StringIO()
e = io.StringIO()
c.seek(0)
print(c, d)
#s1=str(c)
base64.b64encode(bytes(str(c),'gb2312'))# 对StringIO内的数据进行编码

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP