2005年11月11日 星期五 15:38
我同样一个文件,里面有输出中文(中文是从sqlite里读出来的)的语句。
用editplus打开,并保存为gb2312,执行正常。
用pydev(eclipse的插件)打开运行却报错了,不知道在pydev里保存的编码是什么。
代码时这样的:
# -*- coding: utf-8 -*-
from pysqlite2 import dbapi2 as sqlite
# Create a connection to the database file "mydb":
con = sqlite.connect("sqlitedb")
#cur = con.cursor()")
# Get a Cursor object that operates in the context of Connection con:
cur = con.cursor()
SELECT = "select name_last, age from people order by age, name_last"
cur.execute(SELECT)
for (name_last, age) in cur:
print '%s is %d years old.' % (name_last, age)
出错提示:
print '%s is %d years old.' % (name_last, age)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordinal not in range(128)
各位大虾帮忙看看。
ps:哪有讲编码讲得比较透彻的文章?
2005年11月11日 星期五 16:16
在 05-11-11,HoLin<holin.he at gmail.com> 写道: > 我同样一个文件,里面有输出中文(中文是从sqlite里读出来的)的语句。 > 用editplus打开,并保存为gb2312,执行正常。 > 用pydev(eclipse的插件)打开运行却报错了,不知道在pydev里保存的编码是什么。 > 代码时这样的: > # -*- coding: utf-8 -*- > > from pysqlite2 import dbapi2 as sqlite > # Create a connection to the database file "mydb": > con = sqlite.connect("sqlitedb") > #cur = con.cursor()") > # Get a Cursor object that operates in the context of Connection con: > cur = con.cursor() > SELECT = "select name_last, age from people order by age, name_last" > cur.execute(SELECT) > for (name_last, age) in cur: > print '%s is %d years old.' % (name_last, age) > > 出错提示: > print '%s is %d years old.' % (name_last, age) > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-2: ordinal not in range(128) > > 各位大虾帮忙看看。 > pysqlite2中字符串取出来全部是unicode,而shell环境一般都是有编码的,如果不支持unicode的显示的话是会报错的。因此象上面直接打印有可能会出错,可以先转为gbk后再打印。 > ps:哪有讲编码讲得比较透彻的文章? > 本邮件列表中讨论过多次,建议查一查,在wiki上也有一些文件可以查。我们不少人的blog也都有,举不胜举。 -- I like python! My Blog: http://www.donews.net/limodou NewEdit Maillist: http://groups.google.com/group/NewEdit
2005年11月11日 星期五 17:33
用print '%s is %d years old.' % (name_last.encode("gbk"), age) 输出了。
在 05-11-11,limodou<limodou at gmail.com> 写道:
> 在 05-11-11,HoLin<holin.he at gmail.com> 写道:
> > 我同样一个文件,里面有输出中文(中文是从sqlite里读出来的)的语句。
> > 用editplus打开,并保存为gb2312,执行正常。
> > 用pydev(eclipse的插件)打开运行却报错了,不知道在pydev里保存的编码是什么。
> > 代码时这样的:
> > # -*- coding: utf-8 -*-
> >
> > from pysqlite2 import dbapi2 as sqlite
> > # Create a connection to the database file "mydb":
> > con = sqlite.connect("sqlitedb")
> > #cur = con.cursor()")
> > # Get a Cursor object that operates in the context of Connection con:
> > cur = con.cursor()
> > SELECT = "select name_last, age from people order by age, name_last"
> > cur.execute(SELECT)
> > for (name_last, age) in cur:
> > print '%s is %d years old.' % (name_last, age)
> >
> > 出错提示:
> > print '%s is %d years old.' % (name_last, age)
> > UnicodeEncodeError: 'ascii' codec can't encode characters in position
> > 0-2: ordinal not in range(128)
> >
> > 各位大虾帮忙看看。
> >
> pysqlite2中字符串取出来全部是unicode,而shell环境一般都是有编码的,如果不支持unicode的显示的话是会报错的。因此象上面直接打印有可能会出错,可以先转为gbk后再打印。
>
> > ps:哪有讲编码讲得比较透彻的文章?
> >
> 本邮件列表中讨论过多次,建议查一查,在wiki上也有一些文件可以查。我们不少人的blog也都有,举不胜举。
>
> --
> I like python!
> My Blog: http://www.donews.net/limodou
> NewEdit Maillist: http://groups.google.com/group/NewEdit
>
> _______________________________________________
> Python中文技术讨论邮件列表
> 发言: 发邮件到 python-chinese at lists.python.cn
> 订阅: 发送 subscribe 到 python-chinese-request at lists.python.cn
> 退订: 发送 unsubscribe 到 python-chinese-request at lists.python.cn
> 详细说明: http://python.cn/mailman/listinfo/python-chinese
>
>
Zeuux © 2025
京ICP备05028076号