首页 > 解决方案 > UnicodeDecodeError:“utf-8”编解码器无法解码位置 6 中的字节 0xe9:无效的继续字节

问题描述

我在线尝试了许多解决方案,但在我的浏览器http://127.0.0.1:7000/json/json_data_dump上仍然出现相同的错误。有人可以帮忙吗?顺便说一句,如果我在下面做并且只在 Teradata 中插入非法语字符,我的代码就可以工作。

CREATE TABLE tablename(
    Category    varchar(80) not null,         -- CHARACTER SET UNICODE,

下面是python代码:

def json_data_dump(request):
    sql = """ select category as dept from db.tablename order by 1;"""
    df = pd.read_sql(sql, teradata_con())

    df_dept = df[['dept']].drop_duplicates()    

    dic = {'dept':[]}
    for index, row in df_dept.iterrows():        
        dic['dept'].append({                   
                'dept':row['dept'].decode('latin-1').encode("utf-8")   #.encode("utf-8")    #.decode('latin-1')       #decode("utf-8")                                 
            })                

    return HttpResponse(json.dumps(dic,encoding = "ISO-8859-1"))  
    # return HttpResponse(json.dumps(dic,ensure_ascii=False) )     

标签: pythondjangoteradata

解决方案


推荐阅读