[python-chinese] django中文赋值问题求助!
Dreamingk(天成)
dreamingk在gmail.com
星期五 四月 27 14:42:24 HKT 2007
最不地道的是这句
str_zhichu = u''.join(row[0] for row in cu.fetchall())
最好别用fetchall(),可以直接把cursor作为iter来用。这样写
str_zhichu = u''.join(row[0] for row in cu)
On 4/27/07, 黄毅 <yi.codeplayer在gmail.com> wrote:
>
> 我试图从一个数据库表中读取一个字段(该字段内容为中文),在把这个中文赋值给一个字符串变量时,出错:
> > UnicodeDecodeError Exception Value: 'ascii' codec can't decode byte
> > 0xe7 in position 0: ordinal not in range(128)
> > ▼ Local vars <http://127.0.0.1:8000/address/personweekreport/#>
> > Variable Value obj 盈利类
> >
> > 代码如下:
> > str_zhichu = ""
> > cx = sqlite.connect("data.db")
> > cu = cx.cursor()
> > cu.execute("select JobTimeAttr from address_basedict")
> > for row in cu.fetchall():
> > str_zhichu = str_zhichu + row[0] #出错
> >
>
> row[0] 其实是 unicode 对象,就像 limodou 说的,普通字符串str_zhichu会自动解码成unicode,应该是这个时候出的错,把
> str_zhichu = "" 变成 str_zhichu = u"" 应该就 ok 。
>
> 另外你这代码写得可不地道,最后两句循环这样写性能会更好:
> str_zhichu = u''.join(row[0] for row in cu.fetchall())
>
> --
> http://codeplayer.blogspot.com/
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
--
Dreamingk (tc, 天成)
dreamingk(at)gmail.com
http://python.cn
Exoweb (北京轩辕互动科技有限公司)
Python, I love this language.
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070427/71736d11/attachment.htm
关于邮件列表 python-chinese 的更多信息