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) print(type(c)) base64.b64encode(bytes(str(c),'gb2312'))# 对StringIO内的数据进行编码 print (d.getvalue()) d.seek(0) base64.decode(d, e) # 对StringIO内的数据进行解码 print (e.getvalue()) a = "this is a +test" b = base64.urlsafe_b64encode(a) # 进行url的字符串编码
运行代码后后,b = base64.urlsafe_b64encode(a)处报错 TypeError: expected bytes, not 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) print(type(c)) base64.b64encode(bytes(str(c),'gb2312'))# 对StringIO内的数据进行编码 print (d.getvalue()) d.seek(0) base64.decode(d, e) # 对StringIO内的数据进行解码 print (e.getvalue()) a = "this is a +test"b = base64.urlsafe_b64encode(bytes(a,'gb2312'))