python:UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position xxx: ordinal not in range(128)

执行sql_cmd = "select * from item_base where item_id in " + item_ids_str时报错

solve:

import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 

分析:str的默认编码格式为asscii,与utf8的字符item_ids_str串接时,会将item_ids_str安装asscii译码,然后串接。此时会报错。因为item_ids_str是utf8编码的,只能按utf8译码。

因此不要混淆了str的asscii及utf8格式。

原文地址:https://www.cnblogs.com/DjangoBlog/p/3543430.html