Python论坛  - 讨论区

标题:[python-chinese] 如何将UTF-8的编码转成gb2312?

2008年01月03日 星期四 18:50

kun xu coonxu在gmail.com
星期四 一月 3 18:50:04 HKT 2008

ÕÒÁËÒ»ÏÂÏà¹ØµÄÎÄÕ£¬½²µÄ²»ÊÇÌرðÇå³þ£¬Ä¿Ç°ÎҵĻ·¾³ÊÇpython2.3£¬²»ÖªµÀÈçºÎ½«UTF-8µÄ±àÂëת³Égb2312.
»òÕß½øÒ»²½½²£¬ÔÚpythonÏÂÈçºÎʵÏÖ×Ö·û¼¯µÄ±àÂëת»»£¿

ÎÒÖ±½ÓÕâôд£º
gbstring = "´ó¼ÒºÃ"
print gbstring.decode('UTF-8')

±¨´íÁË
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080103/023f8472/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月03日 星期四 18:54

zinicl zinicl在gmail.com
星期四 一月 3 18:54:14 HKT 2008

gbstring.decode('gbk').encode('UTF_8')

ÔÚ08-1-3£¬kun xu <coonxu在gmail.com> дµÀ£º
>
> ÕÒÁËÒ»ÏÂÏà¹ØµÄÎÄÕ£¬½²µÄ²»ÊÇÌرðÇå³þ£¬Ä¿Ç°ÎҵĻ·¾³ÊÇpython2.3£¬²»ÖªµÀÈçºÎ½«UTF-8µÄ±àÂëת³Égb2312.
> »òÕß½øÒ»²½½²£¬ÔÚpythonÏÂÈçºÎʵÏÖ×Ö·û¼¯µÄ±àÂëת»»£¿
>
> ÎÒÖ±½ÓÕâôд£º
> gbstring = "´ó¼ÒºÃ"
> print gbstring.decode('UTF-8')
>
> ±¨´íÁË
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
> ordinal not in range(128)
>
> _______________________________________________
> 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
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080103/c16b62e5/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月03日 星期四 18:56

beck917 beck917在gmail.com
星期四 一月 3 18:56:56 HKT 2008

gbstring.decode('utf-8').encode('gbk')

utf-8 ת gbk

ÔÚ08-1-3£¬kun xu <coonxu在gmail.com> дµÀ£º
>
> ÕÒÁËÒ»ÏÂÏà¹ØµÄÎÄÕ£¬½²µÄ²»ÊÇÌرðÇå³þ£¬Ä¿Ç°ÎҵĻ·¾³ÊÇpython2.3£¬²»ÖªµÀÈçºÎ½«UTF-8µÄ±àÂëת³Égb2312.
> »òÕß½øÒ»²½½²£¬ÔÚpythonÏÂÈçºÎʵÏÖ×Ö·û¼¯µÄ±àÂëת»»£¿
>
> ÎÒÖ±½ÓÕâôд£º
> gbstring = "´ó¼ÒºÃ"
> print gbstring.decode('UTF-8')
>
> ±¨´íÁË
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
> ordinal not in range(128)
>
> _______________________________________________
> 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
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080103/30760427/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月03日 星期四 18:58

beck917 beck917在gmail.com
星期四 一月 3 18:58:53 HKT 2008

:-),ÍíÁËÒ»²½

ÔÚ08-1-3£¬beck917 <beck917在gmail.com> дµÀ£º
>
> gbstring.decode('utf-8').encode('gbk')
>
> utf-8 ת gbk
>
> ÔÚ08-1-3£¬kun xu <coonxu在gmail.com> дµÀ£º
> >
> > ÕÒÁËÒ»ÏÂÏà¹ØµÄÎÄÕ£¬½²µÄ²»ÊÇÌرðÇå³þ£¬Ä¿Ç°ÎҵĻ·¾³ÊÇpython2.3£¬²»ÖªµÀÈçºÎ½«UTF-8µÄ±àÂëת³Égb2312.
> > »òÕß½øÒ»²½½²£¬ÔÚpythonÏÂÈçºÎʵÏÖ×Ö·û¼¯µÄ±àÂëת»»£¿
> >
> > ÎÒÖ±½ÓÕâôд£º
> > gbstring = "´ó¼ÒºÃ"
> > print gbstring.decode('UTF-8')
> >
> > ±¨´íÁË
> > UnicodeEncodeError: 'ascii' codec can't encode characters in position
> > 0-2: ordinal not in range(128)
> >
> > _______________________________________________
> > 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
> >
>
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080103/3cedeb16/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月03日 星期四 19:00

Jiahua Huang jhuangjiahua在gmail.com
星期四 一月 3 19:00:13 HKT 2008

他个报错这是因为直接在 python 源文件里边用中文,
而又没在程序开头写上
# -*- coding: UTF-8 -*-
或
# -*- coding: GB18030 -*-

另外,建议用 utf8 编写文件,
再,需要 gb 的时候请用 gb18030 , 而不是 gb2312。

On Jan 3, 2008 6:54 PM, zinicl <zinicl at gmail.com> wrote:
> gbstring.decode('gbk').encode('UTF_8')
>
> 在08-1-3,kun xu <coonxu at gmail.com> 写道:
> >
> > 找了一下相关的文章,讲的不是特别清楚,目前我的环境是python2.3,不知道如何将UTF-8的编码转成gb2312.
> > 或者进一步讲,在python下如何实现字符集的编码转换?
> >
> > 我直接这么写:
> > gbstring = "大家好"
> > print gbstring.decode('UTF-8')
> >
> > 报错了
> > UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
> ordinal not in range(128)
> >

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2008年01月06日 星期日 17:40

小龙 freefis在gmail.com
星期日 一月 6 17:40:24 HKT 2008

# -*- coding: UTF-8 -*-

str=unicode(str,'gb2312')

ÔÚ08-1-3£¬Jiahua Huang <jhuangjiahua在gmail.com> дµÀ£º
>
> Ëû¸ö±¨´íÕâÊÇÒòΪֱ½ÓÔÚ python Ô´ÎļþÀï±ßÓÃÖÐÎÄ£¬
> ¶øÓÖûÔÚ³ÌÐò¿ªÍ·Ð´ÉÏ
> # -*- coding: UTF-8 -*-
> »ò
> # -*- coding: GB18030 -*-
>
> ÁíÍ⣬½¨ÒéÓà utf8 ±àдÎļþ£¬
> ÔÙ£¬ÐèÒª gb µÄʱºòÇëÓà gb18030 £¬ ¶ø²»ÊÇ gb2312¡£
>
> On Jan 3, 2008 6:54 PM, zinicl <zinicl在gmail.com> wrote:
> > gbstring.decode('gbk').encode('UTF_8')
> >
> > ÔÚ08-1-3£¬kun xu <coonxu在gmail.com> дµÀ£º
> > >
> > > ÕÒÁËÒ»ÏÂÏà¹ØµÄÎÄÕ£¬½²µÄ²»ÊÇÌرðÇå³þ£¬Ä¿Ç°ÎҵĻ·¾³ÊÇpython2.3£¬²»ÖªµÀÈçºÎ½«UTF-8µÄ±àÂëת³Égb2312.
> > > »òÕß½øÒ»²½½²£¬ÔÚpythonÏÂÈçºÎʵÏÖ×Ö·û¼¯µÄ±àÂëת»»£¿
> > >
> > > ÎÒÖ±½ÓÕâôд£º
> > > gbstring = "´ó¼ÒºÃ"
> > > print gbstring.decode('UTF-8')
> > >
> > > ±¨´íÁË
> > > UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 0-2:
> > ordinal not in range(128)
> > >
> _______________________________________________
> 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




-- 
deSign thE  fuTure
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080106/b3747049/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号