如何从sqlite中获取μ字符并进入网页?

时间:2010-01-19 21:31:33

标签: python sqlite unicode utf-8

在使用sqlite数据存储区的Python驱动的Web应用程序上,我遇到了这个错误:

  

无法解码为UTF-8列   'name',文字'300μL-10-10'

阅读here看起来我需要将text-factory切换为str并获取bytestrings,但是当我这样做时,我的html输出如下所示:

  

300L-10-10

我确实将我的内容类型设置为:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

1 个答案:

答案 0 :(得分:3)

不幸的是,数据存储区中的数据未编码为UTF-8;相反,它可能是latin-1或cp1252。要自动解码,请尝试将Connection.text_factory设置为您自己的函数:

def convert_string(s):
    try:
        u = s.decode("utf-8")
    except UnicodeDecodeError:
        u = s.decode("cp1252")
    return u

conn.text_factory = convert_string