|
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内的数据进行编码
|