UnicodeDecodeError:'utf-8'编解码器无法解码位置6的字节0xe9:无效的连续字节

时间:2018-12-14 15:08:46

标签: python django teradata

我在线尝试了多种解决方案,但是在我的浏览器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) )     

0 个答案:

没有答案