python - 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) )
解决方案
推荐阅读
- python - 在模板 django 中过滤查询
- firebase - 检测 .on() 是否正在更新或获取初始数据
- haproxy - 如何在 haproxy 日志中记录后端请求
- c++ - 在 C++ 中测试自定义列表类时卡住了
- python - python多处理读写锁
- java - Java 对 AWS 进行 HTTP 调用
- javascript - 如何在父组件html表格中显示子组件自定义显示元素
- c# - 为什么 Visual Studio 中的“将 JSON 粘贴为类”和“将 XML 粘贴为类”命令被禁用(灰显)?
- python - 编译 Cython 扩展而不调用 python setup.py 或 setuptools
- javascript - 语义 UI - 选项卡/显示 URL