A-A+
TypeError: can only concatenate str (not “bytes”) to str

【注意:此文章为博主原创文章!转载需注意,请带原文链接,至少也要是txt格式!】
python报错:TypeError: can only concatenate str (not "bytes") to str
如下图:
#接收请求数据
def search(request):
request.encoding = 'utf-8'
if 'q' in request.GET:
message = '你搜索的内容为: ' +request.GET['q'].encode('utf-8')
else:
message = '你提交了空表单'
return HttpResponse(message)
代码中标红的位置可以看到使用了encode函数进行了转码,因为encode转码返回的是bytes类型的数据,不可以和str类型的数据直接相加。
由于函数的第一句已经对request请求进行的转码,所以这里我们将后面的encode函数去掉,错误即可解决。
更新后的代码为:
1 2 3 4 5 6 7 8 | #接收请求数据
def search(request):
request.encoding = 'utf-8'
if 'q' in request.GET:
message = '你搜索的内容为: ' +request.GET['q']
else:
message = '你提交了空表单'
return HttpResponse(message) |
以上内容来自:https://www.cnblogs.com/wyhluckdog/p/11362094.html
------------下面是我代码中的错误-----------
错误代码
1 2 3 4 5 6 7 8 | def encrypt2Base64(plainBytes, keyBytes):
if type(plainBytes) is type(""):
plainBytes=plainBytes.encode(encoding="utf-8", errors="strict") ###在这里加了一步判断就解决了错误
cipher = DES.new(keyBytes, DES.MODE_ECB)
plainBytes = pad(plainBytes,DES.block_size,style='pkcs7')
return b64encode(cipher.encrypt(plainBytes))
milliseconds = int(round(time.time() * 1000))
timestamp = encrypt2Base64(str(milliseconds), b'1rkhqaz2') |

布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏