如何将UTF-8的编码转成gb2312?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

如何将UTF-8的编码转成gb2312?

kun xu-3
找了一下相关的文章,讲的不是特别清楚,目前我的环境是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 [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何将UTF-8的编码转成gb2312?

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

在08-1-3,kun xu <[hidden email]> 写道:
找了一下相关的文章,讲的不是特别清楚,目前我的环境是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 [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何将UTF-8的编码转成gb2312?

beck917
In reply to this post by kun xu-3
gbstring.decode('utf-8').encode('gbk')

utf-8 转 gbk

在08-1-3,kun xu <[hidden email]> 写道:
找了一下相关的文章,讲的不是特别清楚,目前我的环境是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 [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何将UTF-8的编码转成gb2312?

beck917
:-),晚了一步

在08-1-3,beck917 <[hidden email]> 写道:
gbstring.decode('utf-8').encode('gbk')

utf-8 转 gbk

在08-1-3,kun xu <[hidden email]> 写道:
找了一下相关的文章,讲的不是特别清楚,目前我的环境是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 [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a href="http://python.cn/mailman/listinfo/python-chinese" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://python.cn/mailman/listinfo/python-chinese



_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何将UTF-8的编码转成gb2312?

Jiahua Huang
In reply to this post by zinicl
他个报错这是因为直接在 python 源文件里边用中文,
而又没在程序开头写上
# -*- coding: UTF-8 -*-

# -*- coding: GB18030 -*-

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

On Jan 3, 2008 6:54 PM, zinicl <[hidden email]> wrote:

> gbstring.decode('gbk').encode('UTF_8')
>
> 在08-1-3,kun xu <[hidden email]> 写道:
> >
> > 找了一下相关的文章,讲的不是特别清楚,目前我的环境是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 [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 如何将UTF-8的编码转成gb2312?

free.wang
# -*- coding: UTF-8 -*-

str=unicode(str,'gb2312')

在08-1-3,Jiahua Huang <[hidden email]> 写道:
他个报错这是因为直接在 python 源文件里边用中文,
而又没在程序开头写上
# -*- coding: UTF-8 -*-

# -*- coding: GB18030 -*-

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

On Jan 3, 2008 6:54 PM, zinicl <[hidden email]> wrote:

> gbstring.decode ('gbk').encode('UTF_8')
>
> 在08-1-3,kun xu <[hidden email]> 写道:
> >
> > 找了一下相关的文章,讲的不是特别清楚,目前我的环境是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 [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese



--
deSign thE  fuTure
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese