Python论坛  - 讨论区

标题:[python-chinese] cjkcodecs on mandrake, a problem

2004年03月17日 星期三 10:12

Anthony Liu antonyliu2002 at yahoo.com
Wed Mar 17 10:12:32 HKT 2004

The superuser of the Mandrake system in my lab
installed cjkcodecs for python.

But there is a problem.

I can encode english alphabets like 'a', 'b', 'c' or
'abc' like so:

s = 'abc'
s = unicode(s, 'gbk')
print s # prints 'abc'

but when I tried to encode Chinese strings, I have a
problem.

For example, to run the following script:

s = "蟒蛇"  # 2 Chinese characters for
python
s = unicode(s, 'gbk')
print s

I get the following error message.

[antony at ancho tmp]$ python snake.py
Traceback (most recent call last):
  File "snake.py", line 6, in ?
    print a
UnicodeEncodeError: 'latin-1' codec can't encode
characters in position 0-1: ordinal not in range(256)
[antony at ancho tmp]$ 

What is the problem please?


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com


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

2004年03月17日 星期三 10:21

Zoom.Quiet zoomq at infopro.cn
Wed Mar 17 10:21:21 HKT 2004

Hello Anthony,

"'latin-1' codec can't encode" is the key!
u say::
s = "蟒蛇"  # 2 Chinese characters for

??
but we can see it as chinese!
so so means in u sys default location is latin-1!!

anything encode as latin-1 ??

so so poor Mandrake.....



=== [ 10:12 ; 04-03-17 ] you wrote:

AL> The superuser of the Mandrake system in my lab
AL> installed cjkcodecs for python.

AL> But there is a problem.

AL> I can encode english alphabets like 'a', 'b', 'c' or
AL> 'abc' like so:

AL> s = 'abc'
AL> s = unicode(s, 'gbk')
AL> print s # prints 'abc'

AL> but when I tried to encode Chinese strings, I have a
AL> problem.

AL> For example, to run the following script:

AL> s = "蟒蛇"  # 2 Chinese characters for
AL> python
AL> s = unicode(s, 'gbk')
AL> print s

AL> I get the following error message.

AL> [antony at ancho tmp]$ python snake.py
AL> Traceback (most recent call last):
AL>   File "snake.py", line 6, in ?
AL>     print a
AL> UnicodeEncodeError: 'latin-1' codec can't encode
AL> characters in position 0-1: ordinal not in range(256)
AL> [antony at ancho tmp]$ 

AL> What is the problem please?


AL> __________________________________
AL> Do you Yahoo!?
AL> Yahoo! Mail - More reliable, more storage, less spam
AL> http://mail.yahoo.com

=== === === === === === === === === === 

-- 
Best regards,
 Zoom.Quiet                            

 /=======================================\
]Time is unimportant, only life important![
 \=======================================/



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

2004年03月17日 星期三 10:28

Who Bruce whoonline at msn.com
Wed Mar 17 10:28:38 HKT 2004

I think you can change your Mandrake's local and language setting to 
Chinese. IIRC, change .lang file


>From: "Zoom.Quiet" <zoomq at infopro.cn>
>Reply-To: "Zoom.Quiet" <zoomq at itcase.com>, zoomq at infopro.cn, 
,python-chinese at lists.python.cn
>To: python-chinese at lists.python.cn
>Subject: Re: [python-chinese] cjkcodecs on mandrake, a problem
>Date: Wed, 17 Mar 2004 10:21:21 +0800
>
>Hello Anthony,
>
>"'latin-1' codec can't encode" is the key!
>u say::
>s = "蟒蛇"  # 2 Chinese characters for
>
>??
>but we can see it as chinese!
>so so means in u sys default location is latin-1!!
>
>anything encode as latin-1 ??
>
>so so poor Mandrake.....
>
>
>
>=== [ 10:12 ; 04-03-17 ] you wrote:
>
>AL> The superuser of the Mandrake system in my lab
>AL> installed cjkcodecs for python.
>
>AL> But there is a problem.
>
>AL> I can encode english alphabets like 'a', 'b', 'c' or
>AL> 'abc' like so:
>
>AL> s = 'abc'
>AL> s = unicode(s, 'gbk')
>AL> print s # prints 'abc'
>
>AL> but when I tried to encode Chinese strings, I have a
>AL> problem.
>
>AL> For example, to run the following script:
>
>AL> s = "蟒蛇"  # 2 Chinese characters for
>AL> python
>AL> s = unicode(s, 'gbk')
>AL> print s
>
>AL> I get the following error message.
>
>AL> [antony at ancho tmp]$ python snake.py
>AL> Traceback (most recent call last):
>AL>   File "snake.py", line 6, in ?
>AL>     print a
>AL> UnicodeEncodeError: 'latin-1' codec can't encode
>AL> characters in position 0-1: ordinal not in range(256)
>AL> [antony at ancho tmp]$
>
>AL> What is the problem please?
>
>
>AL> __________________________________
>AL> Do you Yahoo!?
>AL> Yahoo! Mail - More reliable, more storage, less spam
>AL> http://mail.yahoo.com
>
>=== === === === === === === === === ===
>
>--
>Best regards,
>  Zoom.Quiet
>
>  /=======================================\
>]Time is unimportant, only life important![
>  \=======================================/
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

_________________________________________________________________
免费下载 MSN Explorer:   http://explorer.msn.com/lccn  



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

2004年03月17日 星期三 10:37

Anthony Liu antonyliu2002 at yahoo.com
Wed Mar 17 10:37:39 HKT 2004

Thanks, Bruce.

Where is the .lang file?  Should I create it from
scratch?

And what lines should it contain?

The env command of the Mandrake shows:

LANG=en_US


--- Who Bruce <whoonline at msn.com> wrote:
> I think you can change your Mandrake's local and
> language setting to 
> Chinese. IIRC, change .lang file
> 
> 
> >From: "Zoom.Quiet" <zoomq at infopro.cn>
> >Reply-To: "Zoom.Quiet" <zoomq at itcase.com>,
> zoomq at infopro.cn, 
> ,python-chinese at lists.python.cn
> >To: python-chinese at lists.python.cn
> >Subject: Re: [python-chinese] cjkcodecs on
> mandrake, a problem
> >Date: Wed, 17 Mar 2004 10:21:21 +0800
> >
> >Hello Anthony,
> >
> >"'latin-1' codec can't encode" is the key!
> >u say::
> >s = "蟒蛇"  # 2 Chinese characters for
> >
> >??
> >but we can see it as chinese!
> >so so means in u sys default location is latin-1!!
> >
> >anything encode as latin-1 ??
> >
> >so so poor Mandrake.....
> >
> >
> >
> >=== [ 10:12 ; 04-03-17 ] you wrote:
> >
> >AL> The superuser of the Mandrake system in my lab
> >AL> installed cjkcodecs for python.
> >
> >AL> But there is a problem.
> >
> >AL> I can encode english alphabets like 'a', 'b',
> 'c' or
> >AL> 'abc' like so:
> >
> >AL> s = 'abc'
> >AL> s = unicode(s, 'gbk')
> >AL> print s # prints 'abc'
> >
> >AL> but when I tried to encode Chinese strings, I
> have a
> >AL> problem.
> >
> >AL> For example, to run the following script:
> >
> >AL> s = "蟒蛇"  # 2 Chinese characters
for
> >AL> python
> >AL> s = unicode(s, 'gbk')
> >AL> print s
> >
> >AL> I get the following error message.
> >
> >AL> [antony at ancho tmp]$ python snake.py
> >AL> Traceback (most recent call last):
> >AL>   File "snake.py", line 6, in ?
> >AL>     print a
> >AL> UnicodeEncodeError: 'latin-1' codec can't
> encode
> >AL> characters in position 0-1: ordinal not in
> range(256)
> >AL> [antony at ancho tmp]$
> >
> >AL> What is the problem please?
> >
> >
> >AL> __________________________________
> >AL> Do you Yahoo!?
> >AL> Yahoo! Mail - More reliable, more storage, less
> spam
> >AL> http://mail.yahoo.com
> >
> >=== === === === === === === === === ===
> >
> >--
> >Best regards,
> >  Zoom.Quiet
> >
> >  /=======================================\
> >]Time is unimportant, only life important![
> >  \=======================================/
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> 
>
_________________________________________________________________
> 免费下载 MSN Explorer:  
> http://explorer.msn.com/lccn  
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com


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

2004年03月17日 星期三 10:41

Who Bruce whoonline at msn.com
Wed Mar 17 10:41:36 HKT 2004

IIRC, ~/.lang
edit it
set environment LC_LANG and LC_LOCALE and other essential evironment.
I forget the details,just google it.
good luck

>From: Anthony Liu <antonyliu2002 at yahoo.com>
>Reply-To: python-chinese at lists.python.cn
>To: python-chinese at lists.python.cn
>Subject: Re: [python-chinese] cjkcodecs on mandrake, a problem
>Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
>
>Thanks, Bruce.
>
>Where is the .lang file?  Should I create it from
>scratch?
>
>And what lines should it contain?
>
>The env command of the Mandrake shows:
>
>LANG=en_US
>
>
>--- Who Bruce <whoonline at msn.com> wrote:
> > I think you can change your Mandrake's local and
> > language setting to
> > Chinese. IIRC, change .lang file
> >
> >
> > >From: "Zoom.Quiet" <zoomq at infopro.cn>
> > >Reply-To: "Zoom.Quiet" <zoomq at itcase.com>,
> > zoomq at infopro.cn,
> > ,python-chinese at lists.python.cn
> > >To: python-chinese at lists.python.cn
> > >Subject: Re: [python-chinese] cjkcodecs on
> > mandrake, a problem
> > >Date: Wed, 17 Mar 2004 10:21:21 +0800
> > >
> > >Hello Anthony,
> > >
> > >"'latin-1' codec can't encode" is the key!
> > >u say::
> > >s = "蟒蛇"  # 2 Chinese characters for
> > >
> > >??
> > >but we can see it as chinese!
> > >so so means in u sys default location is latin-1!!
> > >
> > >anything encode as latin-1 ??
> > >
> > >so so poor Mandrake.....
> > >
> > >
> > >
> > >=== [ 10:12 ; 04-03-17 ] you wrote:
> > >
> > >AL> The superuser of the Mandrake system in my lab
> > >AL> installed cjkcodecs for python.
> > >
> > >AL> But there is a problem.
> > >
> > >AL> I can encode english alphabets like 'a', 'b',
> > 'c' or
> > >AL> 'abc' like so:
> > >
> > >AL> s = 'abc'
> > >AL> s = unicode(s, 'gbk')
> > >AL> print s # prints 'abc'
> > >
> > >AL> but when I tried to encode Chinese strings, I
> > have a
> > >AL> problem.
> > >
> > >AL> For example, to run the following script:
> > >
> > >AL> s = "蟒蛇"  # 2 Chinese characters
>for
> > >AL> python
> > >AL> s = unicode(s, 'gbk')
> > >AL> print s
> > >
> > >AL> I get the following error message.
> > >
> > >AL> [antony at ancho tmp]$ python snake.py
> > >AL> Traceback (most recent call last):
> > >AL>   File "snake.py", line 6, in ?
> > >AL>     print a
> > >AL> UnicodeEncodeError: 'latin-1' codec can't
> > encode
> > >AL> characters in position 0-1: ordinal not in
> > range(256)
> > >AL> [antony at ancho tmp]$
> > >
> > >AL> What is the problem please?
> > >
> > >
> > >AL> __________________________________
> > >AL> Do you Yahoo!?
> > >AL> Yahoo! Mail - More reliable, more storage, less
> > spam
> > >AL> http://mail.yahoo.com
> > >
> > >=== === === === === === === === === ===
> > >
> > >--
> > >Best regards,
> > >  Zoom.Quiet
> > >
> > >  /=======================================\
> > >]Time is unimportant, only life important![
> > >  \=======================================/
> > >
> > >_______________________________________________
> > >python-chinese list
> > >python-chinese at lists.python.cn
> > >http://python.cn/mailman/listinfo/python-chinese
> >
> >
>_________________________________________________________________
> > 免费下载 MSN Explorer:
> > http://explorer.msn.com/lccn
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
>
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Mail - More reliable, more storage, less spam
>http://mail.yahoo.com
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

_________________________________________________________________
免费下载 MSN Explorer:   http://explorer.msn.com/lccn  



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

2004年03月17日 星期三 10:44

Anthony Liu antonyliu2002 at yahoo.com
Wed Mar 17 10:44:32 HKT 2004

Can I just call locale.setlocale(locale.LC_ALL,
'zh_CN') in my python script?

--- Who Bruce <whoonline at msn.com> wrote:
> IIRC, ~/.lang
> edit it
> set environment LC_LANG and LC_LOCALE and other
> essential evironment.
> I forget the details,just google it.
> good luck
> 
> >From: Anthony Liu <antonyliu2002 at yahoo.com>
> >Reply-To: python-chinese at lists.python.cn
> >To: python-chinese at lists.python.cn
> >Subject: Re: [python-chinese] cjkcodecs on
> mandrake, a problem
> >Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
> >
> >Thanks, Bruce.
> >
> >Where is the .lang file?  Should I create it from
> >scratch?
> >
> >And what lines should it contain?
> >
> >The env command of the Mandrake shows:
> >
> >LANG=en_US
> >
> >
> >--- Who Bruce <whoonline at msn.com> wrote:
> > > I think you can change your Mandrake's local and
> > > language setting to
> > > Chinese. IIRC, change .lang file
> > >
> > >
> > > >From: "Zoom.Quiet" <zoomq at infopro.cn>
> > > >Reply-To: "Zoom.Quiet" <zoomq at itcase.com>,
> > > zoomq at infopro.cn,
> > > ,python-chinese at lists.python.cn
> > > >To: python-chinese at lists.python.cn
> > > >Subject: Re: [python-chinese] cjkcodecs on
> > > mandrake, a problem
> > > >Date: Wed, 17 Mar 2004 10:21:21 +0800
> > > >
> > > >Hello Anthony,
> > > >
> > > >"'latin-1' codec can't encode" is the key!
> > > >u say::
> > > >s = "蟒蛇"  # 2 Chinese characters
for
> > > >
> > > >??
> > > >but we can see it as chinese!
> > > >so so means in u sys default location is
> latin-1!!
> > > >
> > > >anything encode as latin-1 ??
> > > >
> > > >so so poor Mandrake.....
> > > >
> > > >
> > > >
> > > >=== [ 10:12 ; 04-03-17 ] you wrote:
> > > >
> > > >AL> The superuser of the Mandrake system in my
> lab
> > > >AL> installed cjkcodecs for python.
> > > >
> > > >AL> But there is a problem.
> > > >
> > > >AL> I can encode english alphabets like 'a',
> 'b',
> > > 'c' or
> > > >AL> 'abc' like so:
> > > >
> > > >AL> s = 'abc'
> > > >AL> s = unicode(s, 'gbk')
> > > >AL> print s # prints 'abc'
> > > >
> > > >AL> but when I tried to encode Chinese strings,
> I
> > > have a
> > > >AL> problem.
> > > >
> > > >AL> For example, to run the following script:
> > > >
> > > >AL> s = "蟒蛇"  # 2 Chinese
characters
> >for
> > > >AL> python
> > > >AL> s = unicode(s, 'gbk')
> > > >AL> print s
> > > >
> > > >AL> I get the following error message.
> > > >
> > > >AL> [antony at ancho tmp]$ python snake.py
> > > >AL> Traceback (most recent call last):
> > > >AL>   File "snake.py", line 6, in ?
> > > >AL>     print a
> > > >AL> UnicodeEncodeError: 'latin-1' codec can't
> > > encode
> > > >AL> characters in position 0-1: ordinal not in
> > > range(256)
> > > >AL> [antony at ancho tmp]$
> > > >
> > > >AL> What is the problem please?
> > > >
> > > >
> > > >AL> __________________________________
> > > >AL> Do you Yahoo!?
> > > >AL> Yahoo! Mail - More reliable, more storage,
> less
> > > spam
> > > >AL> http://mail.yahoo.com
> > > >
> > > >=== === === === === === === === === ===
> > > >
> > > >--
> > > >Best regards,
> > > >  Zoom.Quiet
> > > >
> > > >  /=======================================\
> > > >]Time is unimportant, only life important![
> > > >  \=======================================/
> > > >
> > > >_______________________________________________
> > > >python-chinese list
> > > >python-chinese at lists.python.cn
> > >
> >http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
>
>_________________________________________________________________
> > > 免费下载 MSN Explorer:
> > > http://explorer.msn.com/lccn
> > >
> > > _______________________________________________
> > > python-chinese list
> > > python-chinese at lists.python.cn
> > > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >__________________________________
> >Do you Yahoo!?
> >Yahoo! Mail - More reliable, more storage, less
> spam
> >http://mail.yahoo.com
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> 
>
_________________________________________________________________
> 免费下载 MSN Explorer:  
> http://explorer.msn.com/lccn  
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com


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

2004年03月17日 星期三 11:02

Zoom.Quiet zoomq at infopro.cn
Wed Mar 17 11:02:41 HKT 2004

Hello Anthony,

after Python 2.3.x
u can insert line::
"""
# -*- coding: utf-8 -*-
"""
at script 1st

to make Python know what encode u will write

=== [ 10:44 ; 04-03-17 ] you wrote:

AL> Can I just call locale.setlocale(locale.LC_ALL,
AL> 'zh_CN') in my python script?

AL> --- Who Bruce <whoonline at msn.com> wrote:
>> IIRC, ~/.lang
>> edit it
>> set environment LC_LANG and LC_LOCALE and other
>> essential evironment.
>> I forget the details,just google it.
>> good luck
>> 
>> >From: Anthony Liu <antonyliu2002 at yahoo.com>
>> >Reply-To: python-chinese at lists.python.cn
>> >To: python-chinese at lists.python.cn
>> >Subject: Re: [python-chinese] cjkcodecs on
>> mandrake, a problem
>> >Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
>> >
>> >Thanks, Bruce.
>> >
>> >Where is the .lang file?  Should I create it from
>> >scratch?
>> >
>> >And what lines should it contain?
>> >
>> >The env command of the Mandrake shows:
>> >
>> >LANG=en_US
>> >
>> >
>> >--- Who Bruce <whoonline at msn.com> wrote:
>> > > I think you can change your Mandrake's local and
>> > > language setting to
>> > > Chinese. IIRC, change .lang file
>> > >
>> > >
>> > > >From: "Zoom.Quiet" <zoomq at infopro.cn>
>> > > >Reply-To: "Zoom.Quiet" <zoomq at itcase.com>,
>> > > zoomq at infopro.cn,
>> > > ,python-chinese at lists.python.cn
>> > > >To: python-chinese at lists.python.cn
>> > > >Subject: Re: [python-chinese] cjkcodecs on
>> > > mandrake, a problem
>> > > >Date: Wed, 17 Mar 2004 10:21:21 +0800
>> > > >
>> > > >Hello Anthony,
>> > > >
>> > > >"'latin-1' codec can't encode" is the key!
>> > > >u say::
>> > > >s = "蟒蛇"  # 2 Chinese characters
AL> for
>> > > >
>> > > >??
>> > > >but we can see it as chinese!
>> > > >so so means in u sys default location is
>> latin-1!!
>> > > >
>> > > >anything encode as latin-1 ??
>> > > >
>> > > >so so poor Mandrake.....
>> > > >
>> > > >
>> > > >
>> > > >=== [ 10:12 ; 04-03-17 ] you wrote:
>> > > >
>> > > >AL> The superuser of the Mandrake system in my
>> lab
>> > > >AL> installed cjkcodecs for python.
>> > > >
>> > > >AL> But there is a problem.
>> > > >
>> > > >AL> I can encode english alphabets like 'a',
>> 'b',
>> > > 'c' or
>> > > >AL> 'abc' like so:
>> > > >
>> > > >AL> s = 'abc'
>> > > >AL> s = unicode(s, 'gbk')
>> > > >AL> print s # prints 'abc'
>> > > >
>> > > >AL> but when I tried to encode Chinese strings,
>> I
>> > > have a
>> > > >AL> problem.
>> > > >
>> > > >AL> For example, to run the following script:
>> > > >
>> > > >AL> s = "蟒蛇"  # 2 Chinese
AL> characters
>> >for
>> > > >AL> python
>> > > >AL> s = unicode(s, 'gbk')
>> > > >AL> print s
>> > > >
>> > > >AL> I get the following error message.
>> > > >
>> > > >AL> [antony at ancho tmp]$ python snake.py
>> > > >AL> Traceback (most recent call last):
>> > > >AL>   File "snake.py", line 6, in ?
>> > > >AL>     print a
>> > > >AL> UnicodeEncodeError: 'latin-1' codec can't
>> > > encode
>> > > >AL> characters in position 0-1: ordinal not in
>> > > range(256)
>> > > >AL> [antony at ancho tmp]$
>> > > >
>> > > >AL> What is the problem please?
>> > > >
>> > > >
>> > > >AL> __________________________________
>> > > >AL> Do you Yahoo!?
>> > > >AL> Yahoo! Mail - More reliable, more storage,
>> less
>> > > spam
>> > > >AL> http://mail.yahoo.com
>> > > >
>> > > >=== === === === === === === === === ===
>> > > >
>> > > >--
>> > > >Best regards,
>> > > >  Zoom.Quiet
>> > > >
>> > > >  /=======================================\
>> > > >]Time is unimportant, only life important![
>> > > >  \=======================================/
>> > > >
>> > > >_______________________________________________
>> > > >python-chinese list
>> > > >python-chinese at lists.python.cn
>> > >
>> >http://python.cn/mailman/listinfo/python-chinese
>> > >
>> > >
>>
>>_________________________________________________________________
>> > > 免费下载 MSN Explorer:
>> > > http://explorer.msn.com/lccn
>> > >
>> > > _______________________________________________
>> > > python-chinese list
>> > > python-chinese at lists.python.cn
>> > > http://python.cn/mailman/listinfo/python-chinese
>> >
>> >
>> >__________________________________
>> >Do you Yahoo!?
>> >Yahoo! Mail - More reliable, more storage, less
>> spam
>> >http://mail.yahoo.com
>> >_______________________________________________
>> >python-chinese list
>> >python-chinese at lists.python.cn
>> >http://python.cn/mailman/listinfo/python-chinese
>> 
>>
AL> _________________________________________________________________
>> 免费下载 MSN Explorer:  
>> http://explorer.msn.com/lccn  
>> 
>> _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese


AL> __________________________________
AL> Do you Yahoo!?
AL> Yahoo! Mail - More reliable, more storage, less spam
AL> http://mail.yahoo.com

=== === === === === === === === === === 

-- 
Best regards,
 Zoom.Quiet                            

 /=======================================\
]Time is unimportant, only life important![
 \=======================================/



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

2004年03月17日 星期三 11:18

Anthony Liu antonyliu2002 at yahoo.com
Wed Mar 17 11:18:50 HKT 2004

"Zoom.Quiet" <zoomq at itcase.com>, zoomq at infopro.cn,
INVALID_ADDRESS at .SYNTAX-ERROR.

does not work.  look at this:

# -*- coding: utf-8 -*-               

a = '蟒蛇'
b = 'abc'
a = unicode(a, 'utf-8')
print a

Traceback (most recent call last):
  File "snake.py", line 8, in ?
    a = unicode(a, 'utf-8')
UnicodeDecodeError: 'utf8' codec can't decode bytes in
position 0-3: invalid data

--- "Zoom.Quiet" <zoomq at infopro.cn> wrote:
> Hello Anthony,
> 
> after Python 2.3.x
> u can insert line::
> """
> # -*- coding: utf-8 -*-
> """
> at script 1st
> 
> to make Python know what encode u will write
> 
> === [ 10:44 ; 04-03-17 ] you wrote:
> 
> AL> Can I just call locale.setlocale(locale.LC_ALL,
> AL> 'zh_CN') in my python script?
> 
> AL> --- Who Bruce <whoonline at msn.com> wrote:
> >> IIRC, ~/.lang
> >> edit it
> >> set environment LC_LANG and LC_LOCALE and other
> >> essential evironment.
> >> I forget the details,just google it.
> >> good luck
> >> 
> >> >From: Anthony Liu <antonyliu2002 at yahoo.com>
> >> >Reply-To: python-chinese at lists.python.cn
> >> >To: python-chinese at lists.python.cn
> >> >Subject: Re: [python-chinese] cjkcodecs on
> >> mandrake, a problem
> >> >Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
> >> >
> >> >Thanks, Bruce.
> >> >
> >> >Where is the .lang file?  Should I create it
> from
> >> >scratch?
> >> >
> >> >And what lines should it contain?
> >> >
> >> >The env command of the Mandrake shows:
> >> >
> >> >LANG=en_US
> >> >
> >> >
> >> >--- Who Bruce <whoonline at msn.com> wrote:
> >> > > I think you can change your Mandrake's local
> and
> >> > > language setting to
> >> > > Chinese. IIRC, change .lang file
> >> > >
> >> > >
> >> > > >From: "Zoom.Quiet" <zoomq at infopro.cn>
> >> > > >Reply-To: "Zoom.Quiet" <zoomq at itcase.com>,
> >> > > zoomq at infopro.cn,
> >> > > ,python-chinese at lists.python.cn
> >> > > >To: python-chinese at lists.python.cn
> >> > > >Subject: Re: [python-chinese] cjkcodecs on
> >> > > mandrake, a problem
> >> > > >Date: Wed, 17 Mar 2004 10:21:21 +0800
> >> > > >
> >> > > >Hello Anthony,
> >> > > >
> >> > > >"'latin-1' codec can't encode" is the key!
> >> > > >u say::
> >> > > >s = "蟒蛇"  # 2 Chinese
> characters
> AL> for
> >> > > >
> >> > > >??
> >> > > >but we can see it as chinese!
> >> > > >so so means in u sys default location is
> >> latin-1!!
> >> > > >
> >> > > >anything encode as latin-1 ??
> >> > > >
> >> > > >so so poor Mandrake.....
> >> > > >
> >> > > >
> >> > > >
> >> > > >=== [ 10:12 ; 04-03-17 ] you wrote:
> >> > > >
> >> > > >AL> The superuser of the Mandrake system in
> my
> >> lab
> >> > > >AL> installed cjkcodecs for python.
> >> > > >
> >> > > >AL> But there is a problem.
> >> > > >
> >> > > >AL> I can encode english alphabets like 'a',
> >> 'b',
> >> > > 'c' or
> >> > > >AL> 'abc' like so:
> >> > > >
> >> > > >AL> s = 'abc'
> >> > > >AL> s = unicode(s, 'gbk')
> >> > > >AL> print s # prints 'abc'
> >> > > >
> >> > > >AL> but when I tried to encode Chinese
> strings,
> >> I
> >> > > have a
> >> > > >AL> problem.
> >> > > >
> >> > > >AL> For example, to run the following
> script:
> >> > > >
> >> > > >AL> s = "蟒蛇"  # 2 Chinese
> AL> characters
> >> >for
> >> > > >AL> python
> >> > > >AL> s = unicode(s, 'gbk')
> >> > > >AL> print s
> >> > > >
> >> > > >AL> I get the following error message.
> >> > > >
> >> > > >AL> [antony at ancho tmp]$ python snake.py
> >> > > >AL> Traceback (most recent call last):
> >> > > >AL>   File "snake.py", line 6, in ?
> >> > > >AL>     print a
> >> > > >AL> UnicodeEncodeError: 'latin-1' codec
> can't
> >> > > encode
> >> > > >AL> characters in position 0-1: ordinal not
> in
> >> > > range(256)
> >> > > >AL> [antony at ancho tmp]$
> >> > > >
> >> > > >AL> What is the problem please?
> >> > > >
> >> > > >
> >> > > >AL> __________________________________
> >> > > >AL> Do you Yahoo!?
> >> > > >AL> Yahoo! Mail - More reliable, more
> storage,
> >> less
> >> > > spam
> >> > > >AL> http://mail.yahoo.com
> >> > > >
> >> > > >=== === === === === === === === === ===
> >> > > >
> >> > > >--
> >> > > >Best regards,
> >> > > >  Zoom.Quiet
> >> > > >
> >> > > >  /=======================================\
> >> > > >]Time is unimportant, only life important![
> >> > > >  \=======================================/
> >> > > >
> >> > >
> >_______________________________________________
> >> > > >python-chinese list
> >> > > >python-chinese at lists.python.cn
> >> > >
> >> >http://python.cn/mailman/listinfo/python-chinese
> >> > >
> >> > >
> >>
>
>>_________________________________________________________________
> >> > > 免费下载 MSN
> Explorer:
> >> > > http://explorer.msn.com/lccn
> >> > >
> >> > >
> _______________________________________________
> >> > > python-chinese list
> >> > > python-chinese at lists.python.cn
> >> > >
> http://python.cn/mailman/listinfo/python-chinese
> >> >
> >> >
> >> >__________________________________
> >> >Do you Yahoo!?
> >> >Yahoo! Mail - More reliable, more storage, less
> >> spam
> >> >http://mail.yahoo.com
> >> >_______________________________________________
> >> >python-chinese list
> >> >python-chinese at lists.python.cn
> >> >http://python.cn/mailman/listinfo/python-chinese
> >> 
> >>
> AL>
>
_________________________________________________________________
> >> 免费下载 MSN Explorer:  
> >> http://explorer.msn.com/lccn  
> >> 
> >> _______________________________________________
> >> python-chinese list
> >> python-chinese at lists.python.cn
> >> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> AL> __________________________________
> AL> Do you Yahoo!?
> AL> Yahoo! Mail - More reliable, more storage, less
> spam
> AL> http://mail.yahoo.com
> 
> === === === === === === === === === === 
> 
> -- 
> 
=== message truncated ===>
_______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com


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

2004年03月17日 星期三 11:43

Zoom.Quiet zoomq at infopro.cn
Wed Mar 17 11:43:44 HKT 2004

Hello Anthony,

if define
# -*- coding: utf-8 -*-

u editor must can import unicode code;
not like the '蟒蛇' ASII  utf-8 encode codes!!!


=== [ 11:18 ; 04-03-17 ] you wrote:

AL> "Zoom.Quiet" <zoomq at itcase.com>, zoomq at infopro.cn,
AL> INVALID_ADDRESS at .SYNTAX-ERROR.

AL> does not work.  look at this:

AL> # -*- coding: utf-8 -*-               

AL> a = '蟒蛇'
AL> b = 'abc'
AL> a = unicode(a, 'utf-8')
AL> print a

AL> Traceback (most recent call last):
AL>   File "snake.py", line 8, in ?
AL>     a = unicode(a, 'utf-8')
AL> UnicodeDecodeError: 'utf8' codec can't decode bytes in
AL> position 0-3: invalid data

AL> --- "Zoom.Quiet" <zoomq at infopro.cn> wrote:
>> Hello Anthony,
>> 
>> after Python 2.3.x
>> u can insert line::
>> """
>> # -*- coding: utf-8 -*-
>> """
>> at script 1st
>> 
>> to make Python know what encode u will write
>> 
>> === [ 10:44 ; 04-03-17 ] you wrote:
>> 
>> AL> Can I just call locale.setlocale(locale.LC_ALL,
>> AL> 'zh_CN') in my python script?
>> 
>> AL> --- Who Bruce <whoonline at msn.com> wrote:
>> >> IIRC, ~/.lang
>> >> edit it
>> >> set environment LC_LANG and LC_LOCALE and other
>> >> essential evironment.
>> >> I forget the details,just google it.
>> >> good luck
>> >> 
>> >> >From: Anthony Liu <antonyliu2002 at yahoo.com>
>> >> >Reply-To: python-chinese at lists.python.cn
>> >> >To: python-chinese at lists.python.cn
>> >> >Subject: Re: [python-chinese] cjkcodecs on
>> >> mandrake, a problem
>> >> >Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
>> >> >
>> >> >Thanks, Bruce.
>> >> >
>> >> >Where is the .lang file?  Should I create it
>> from
>> >> >scratch?
>> >> >
>> >> >And what lines should it contain?
>> >> >
>> >> >The env command of the Mandrake shows:
>> >> >
>> >> >LANG=en_US
>> >> >
>> >> >
>> >> >--- Who Bruce <whoonline at msn.com> wrote:
>> >> > > I think you can change your Mandrake's local
>> and
>> >> > > language setting to
>> >> > > Chinese. IIRC, change .lang file
>> >> > >
>> >> > >
>> >> > > >From: "Zoom.Quiet" <zoomq at infopro.cn>
>> >> > > >Reply-To: "Zoom.Quiet" <zoomq at itcase.com>,
>> >> > > zoomq at infopro.cn,
>> >> > > ,python-chinese at lists.python.cn
>> >> > > >To: python-chinese at lists.python.cn
>> >> > > >Subject: Re: [python-chinese] cjkcodecs on
>> >> > > mandrake, a problem
>> >> > > >Date: Wed, 17 Mar 2004 10:21:21 +0800
>> >> > > >
>> >> > > >Hello Anthony,
>> >> > > >
>> >> > > >"'latin-1' codec can't encode" is the key!
>> >> > > >u say::
>> >> > > >s = "蟒蛇"  # 2 Chinese
>> characters
>> AL> for
>> >> > > >
>> >> > > >??
>> >> > > >but we can see it as chinese!
>> >> > > >so so means in u sys default location is
>> >> latin-1!!
>> >> > > >
>> >> > > >anything encode as latin-1 ??
>> >> > > >
>> >> > > >so so poor Mandrake.....
>> >> > > >
>> >> > > >
>> >> > > >
>> >> > > >=== [ 10:12 ; 04-03-17 ] you wrote:
>> >> > > >
>> >> > > >AL> The superuser of the Mandrake system in
>> my
>> >> lab
>> >> > > >AL> installed cjkcodecs for python.
>> >> > > >
>> >> > > >AL> But there is a problem.
>> >> > > >
>> >> > > >AL> I can encode english alphabets like 'a',
>> >> 'b',
>> >> > > 'c' or
>> >> > > >AL> 'abc' like so:
>> >> > > >
>> >> > > >AL> s = 'abc'
>> >> > > >AL> s = unicode(s, 'gbk')
>> >> > > >AL> print s # prints 'abc'
>> >> > > >
>> >> > > >AL> but when I tried to encode Chinese
>> strings,
>> >> I
>> >> > > have a
>> >> > > >AL> problem.
>> >> > > >
>> >> > > >AL> For example, to run the following
>> script:
>> >> > > >
>> >> > > >AL> s = "蟒蛇"  # 2 Chinese
>> AL> characters
>> >> >for
>> >> > > >AL> python
>> >> > > >AL> s = unicode(s, 'gbk')
>> >> > > >AL> print s
>> >> > > >
>> >> > > >AL> I get the following error message.
>> >> > > >
>> >> > > >AL> [antony at ancho tmp]$ python snake.py
>> >> > > >AL> Traceback (most recent call last):
>> >> > > >AL>   File "snake.py", line 6, in ?
>> >> > > >AL>     print a
>> >> > > >AL> UnicodeEncodeError: 'latin-1' codec
>> can't
>> >> > > encode
>> >> > > >AL> characters in position 0-1: ordinal not
>> in
>> >> > > range(256)
>> >> > > >AL> [antony at ancho tmp]$
>> >> > > >
>> >> > > >AL> What is the problem please?
>> >> > > >
>> >> > > >
>> >> > > >AL> __________________________________
>> >> > > >AL> Do you Yahoo!?
>> >> > > >AL> Yahoo! Mail - More reliable, more
>> storage,
>> >> less
>> >> > > spam
>> >> > > >AL> http://mail.yahoo.com
>> >> > > >
>> >> > > >=== === === === === === === === === ===
>> >> > > >
>> >> > > >--
>> >> > > >Best regards,
>> >> > > >  Zoom.Quiet
>> >> > > >
>> >> > > >  /=======================================\
>> >> > > >]Time is unimportant, only life important![
>> >> > > >  \=======================================/
>> >> > > >
>> >> > >
>> >_______________________________________________
>> >> > > >python-chinese list
>> >> > > >python-chinese at lists.python.cn
>> >> > >
>> >> >http://python.cn/mailman/listinfo/python-chinese
>> >> > >
>> >> > >
>> >>
>>
>>>_________________________________________________________________
>> >> > > 免费下载 MSN
>> Explorer:
>> >> > > http://explorer.msn.com/lccn
>> >> > >
>> >> > >
>> _______________________________________________
>> >> > > python-chinese list
>> >> > > python-chinese at lists.python.cn
>> >> > >
>> http://python.cn/mailman/listinfo/python-chinese
>> >> >
>> >> >
>> >> >__________________________________
>> >> >Do you Yahoo!?
>> >> >Yahoo! Mail - More reliable, more storage, less
>> >> spam
>> >> >http://mail.yahoo.com
>> >> >_______________________________________________
>> >> >python-chinese list
>> >> >python-chinese at lists.python.cn
>> >> >http://python.cn/mailman/listinfo/python-chinese
>> >> 
>> >>
>> AL>
>>
AL> _________________________________________________________________
>> >> 免费下载 MSN Explorer:  
>> >> http://explorer.msn.com/lccn  
>> >> 
>> >> _______________________________________________
>> >> python-chinese list
>> >> python-chinese at lists.python.cn
>> >> http://python.cn/mailman/listinfo/python-chinese
>> 
>> 
>> AL> __________________________________
>> AL> Do you Yahoo!?
>> AL> Yahoo! Mail - More reliable, more storage, less
>> spam
>> AL> http://mail.yahoo.com
>> 
>> === === === === === === === === === === 
>> 
>> -- 
>> 
AL> === message truncated ===>
AL> _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>> 


AL> __________________________________
AL> Do you Yahoo!?
AL> Yahoo! Mail - More reliable, more storage, less spam
AL> http://mail.yahoo.com

=== === === === === === === === === === 

-- 
Best regards,
 Zoom.Quiet                            

 /=======================================\
]Time is unimportant, only life important![
 \=======================================/



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

2004年03月17日 星期三 11:47

Anthony Liu antonyliu2002 at yahoo.com
Wed Mar 17 11:47:27 HKT 2004

OK, but how do I know what encoding is being used for
a chinese document?

I will read in some Chinese text files and then
process them.


"Zoom.Quiet" <zoomq at itcase.com>, zoomq at infopro.cn,
INVALID_ADDRESS at .SYNTAX-ERROR.

--- "Zoom.Quiet" <zoomq at infopro.cn> wrote:
> Hello Anthony,
> 
> if define
> # -*- coding: utf-8 -*-
> 
> u editor must can import unicode code;
> not like the '蟒蛇' ASII  utf-8 encode
> codes!!!
> 
> 
> === [ 11:18 ; 04-03-17 ] you wrote:
> 
> AL> "Zoom.Quiet" <zoomq at itcase.com>,
> zoomq at infopro.cn,
> AL> INVALID_ADDRESS at .SYNTAX-ERROR.
> 
> AL> does not work.  look at this:
> 
> AL> # -*- coding: utf-8 -*-               
> 
> AL> a = '蟒蛇'
> AL> b = 'abc'
> AL> a = unicode(a, 'utf-8')
> AL> print a
> 
> AL> Traceback (most recent call last):
> AL>   File "snake.py", line 8, in ?
> AL>     a = unicode(a, 'utf-8')
> AL> UnicodeDecodeError: 'utf8' codec can't decode
> bytes in
> AL> position 0-3: invalid data
> 
> AL> --- "Zoom.Quiet" <zoomq at infopro.cn> wrote:
> >> Hello Anthony,
> >> 
> >> after Python 2.3.x
> >> u can insert line::
> >> """
> >> # -*- coding: utf-8 -*-
> >> """
> >> at script 1st
> >> 
> >> to make Python know what encode u will write
> >> 
> >> === [ 10:44 ; 04-03-17 ] you wrote:
> >> 
> >> AL> Can I just call
> locale.setlocale(locale.LC_ALL,
> >> AL> 'zh_CN') in my python script?
> >> 
> >> AL> --- Who Bruce <whoonline at msn.com> wrote:
> >> >> IIRC, ~/.lang
> >> >> edit it
> >> >> set environment LC_LANG and LC_LOCALE and
> other
> >> >> essential evironment.
> >> >> I forget the details,just google it.
> >> >> good luck
> >> >> 
> >> >> >From: Anthony Liu <antonyliu2002 at yahoo.com>
> >> >> >Reply-To: python-chinese at lists.python.cn
> >> >> >To: python-chinese at lists.python.cn
> >> >> >Subject: Re: [python-chinese] cjkcodecs on
> >> >> mandrake, a problem
> >> >> >Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
> >> >> >
> >> >> >Thanks, Bruce.
> >> >> >
> >> >> >Where is the .lang file?  Should I create it
> >> from
> >> >> >scratch?
> >> >> >
> >> >> >And what lines should it contain?
> >> >> >
> >> >> >The env command of the Mandrake shows:
> >> >> >
> >> >> >LANG=en_US
> >> >> >
> >> >> >
> >> >> >--- Who Bruce <whoonline at msn.com> wrote:
> >> >> > > I think you can change your Mandrake's
> local
> >> and
> >> >> > > language setting to
> >> >> > > Chinese. IIRC, change .lang file
> >> >> > >
> >> >> > >
> >> >> > > >From: "Zoom.Quiet" <zoomq at infopro.cn>
> >> >> > > >Reply-To: "Zoom.Quiet"
> <zoomq at itcase.com>,
> >> >> > > zoomq at infopro.cn,
> >> >> > > ,python-chinese at lists.python.cn
> >> >> > > >To: python-chinese at lists.python.cn
> >> >> > > >Subject: Re: [python-chinese] cjkcodecs
> on
> >> >> > > mandrake, a problem
> >> >> > > >Date: Wed, 17 Mar 2004 10:21:21 +0800
> >> >> > > >
> >> >> > > >Hello Anthony,
> >> >> > > >
> >> >> > > >"'latin-1' codec can't encode" is the
> key!
> >> >> > > >u say::
> >> >> > > >s = "蟒蛇"  # 2 Chinese
> >> characters
> >> AL> for
> >> >> > > >
> >> >> > > >??
> >> >> > > >but we can see it as chinese!
> >> >> > > >so so means in u sys default location is
> >> >> latin-1!!
> >> >> > > >
> >> >> > > >anything encode as latin-1 ??
> >> >> > > >
> >> >> > > >so so poor Mandrake.....
> >> >> > > >
> >> >> > > >
> >> >> > > >
> >> >> > > >=== [ 10:12 ; 04-03-17 ] you wrote:
> >> >> > > >
> >> >> > > >AL> The superuser of the Mandrake system
> in
> >> my
> >> >> lab
> >> >> > > >AL> installed cjkcodecs for python.
> >> >> > > >
> >> >> > > >AL> But there is a problem.
> >> >> > > >
> >> >> > > >AL> I can encode english alphabets like
> 'a',
> >> >> 'b',
> >> >> > > 'c' or
> >> >> > > >AL> 'abc' like so:
> >> >> > > >
> >> >> > > >AL> s = 'abc'
> >> >> > > >AL> s = unicode(s, 'gbk')
> >> >> > > >AL> print s # prints 'abc'
> >> >> > > >
> >> >> > > >AL> but when I tried to encode Chinese
> >> strings,
> >> >> I
> >> >> > > have a
> >> >> > > >AL> problem.
> >> >> > > >
> >> >> > > >AL> For example, to run the following
> >> script:
> >> >> > > >
> >> >> > > >AL> s = "蟒蛇"  # 2 Chinese
> >> AL> characters
> >> >> >for
> >> >> > > >AL> python
> >> >> > > >AL> s = unicode(s, 'gbk')
> >> >> > > >AL> print s
> >> >> > > >
> >> >> > > >AL> I get the following error message.
> >> >> > > >
> >> >> > > >AL> [antony at ancho tmp]$ python snake.py
> >> >> > > >AL> Traceback (most recent call last):
> >> >> > > >AL>   File "snake.py", line 6, in ?
> >> >> > > >AL>     print a
> >> >> > > >AL> UnicodeEncodeError: 'latin-1' codec
> >> can't
> >> >> > > encode
> >> >> > > >AL> characters in position 0-1: ordinal
> not
> >> in
> >> >> > > range(256)
> >> >> > > >AL> [antony at ancho tmp]$
> >> >> > > >
> >> >> > > >AL> What is the problem please?
> >> >> > > >
> >> >> > > >
> >> >> > > >AL> __________________________________
> >> >> > > >AL> Do you Yahoo!?
> >> >> > > >AL> Yahoo! Mail - More reliable, more
> >> storage,
> >> >> less
> >> >> > > spam
> >> >> > > >AL> http://mail.yahoo.com
> >> >> > > >
> >> >> > > >=== === === === === === === === === ===
> >> >> > > >
> >> >> > > >--
> >> >> > > >Best regards,
> >> >> > > >  Zoom.Quiet
> >> >> > > >
> >> >> > > > 
> /=======================================\
> >> >> > > >]Time is unimportant, only life
> important![
> >> >> > > > 
> \=======================================/
> >> >> > > >
> >> >> > >
> >> >_______________________________________________
> >> >> > > >python-chinese list
> >> >> > > >python-chinese at lists.python.cn
> >> >> > >
> >> >>
> >http://python.cn/mailman/listinfo/python-chinese
> >> >> > >
> 
=== message truncated ===>
_______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com


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

2004年03月17日 星期三 15:28

Anthony Liu antonyliu2002 at yahoo.com
Wed Mar 17 15:28:27 HKT 2004

I've added

export LANG=zh_CN.GB2312

in both .bash_profile and .bashrc

Now, if I issue 'locale', I get:

LANG=zh_CN.GB2312
LC_CTYPE=en_US
LC_NUMERIC=en_US
LC_TIME=en_US
LC_COLLATE=en_US
LC_MONETARY=en_US
LC_MESSAGES=en_US
LC_PAPER=en_US
LC_NAME=en_US
LC_ADDRESS=en_US
LC_TELEPHONE=en_US
LC_MEASUREMENT=en_US
LC_IDENTIFICATION=en_US
LC_ALL=

Notice that LANG=zh_CN.GB2312, which seems to be what
I want, but still when I run the following script:

s = '蟒蛇' # s is a string in Chinese
s = unicode(s, 'gb2312')
print s

I still get the same error message:

UnicodeEncodeError: 'latin-1' codec can't encode
characters in position 0-1: ordinal not in range(256).

What should I do?  Thank you!


--- "Zoom.Quiet" <zoomq at infopro.cn> wrote:
> Hello Anthony,
> 
> if define
> # -*- coding: utf-8 -*-
> 
> u editor must can import unicode code;
> not like the '蟒蛇' ASII  utf-8 encode
> codes!!!
> 
> 
> === [ 11:18 ; 04-03-17 ] you wrote:
> 
> AL> "Zoom.Quiet" <zoomq at itcase.com>,
> zoomq at infopro.cn,
> AL> INVALID_ADDRESS at .SYNTAX-ERROR.
> 
> AL> does not work.  look at this:
> 
> AL> # -*- coding: utf-8 -*-               
> 
> AL> a = '蟒蛇'
> AL> b = 'abc'
> AL> a = unicode(a, 'utf-8')
> AL> print a
> 
> AL> Traceback (most recent call last):
> AL>   File "snake.py", line 8, in ?
> AL>     a = unicode(a, 'utf-8')
> AL> UnicodeDecodeError: 'utf8' codec can't decode
> bytes in
> AL> position 0-3: invalid data
> 
> AL> --- "Zoom.Quiet" <zoomq at infopro.cn> wrote:
> >> Hello Anthony,
> >> 
> >> after Python 2.3.x
> >> u can insert line::
> >> """
> >> # -*- coding: utf-8 -*-
> >> """
> >> at script 1st
> >> 
> >> to make Python know what encode u will write
> >> 
> >> === [ 10:44 ; 04-03-17 ] you wrote:
> >> 
> >> AL> Can I just call
> locale.setlocale(locale.LC_ALL,
> >> AL> 'zh_CN') in my python script?
> >> 
> >> AL> --- Who Bruce <whoonline at msn.com> wrote:
> >> >> IIRC, ~/.lang
> >> >> edit it
> >> >> set environment LC_LANG and LC_LOCALE and
> other
> >> >> essential evironment.
> >> >> I forget the details,just google it.
> >> >> good luck
> >> >> 
> >> >> >From: Anthony Liu <antonyliu2002 at yahoo.com>
> >> >> >Reply-To: python-chinese at lists.python.cn
> >> >> >To: python-chinese at lists.python.cn
> >> >> >Subject: Re: [python-chinese] cjkcodecs on
> >> >> mandrake, a problem
> >> >> >Date: Tue, 16 Mar 2004 18:37:39 -0800 (PST)
> >> >> >
> >> >> >Thanks, Bruce.
> >> >> >
> >> >> >Where is the .lang file?  Should I create it
> >> from
> >> >> >scratch?
> >> >> >
> >> >> >And what lines should it contain?
> >> >> >
> >> >> >The env command of the Mandrake shows:
> >> >> >
> >> >> >LANG=en_US
> >> >> >
> >> >> >
> >> >> >--- Who Bruce <whoonline at msn.com> wrote:
> >> >> > > I think you can change your Mandrake's
> local
> >> and
> >> >> > > language setting to
> >> >> > > Chinese. IIRC, change .lang file
> >> >> > >
> >> >> > >
> >> >> > > >From: "Zoom.Quiet" <zoomq at infopro.cn>
> >> >> > > >Reply-To: "Zoom.Quiet"
> <zoomq at itcase.com>,
> >> >> > > zoomq at infopro.cn,
> >> >> > > ,python-chinese at lists.python.cn
> >> >> > > >To: python-chinese at lists.python.cn
> >> >> > > >Subject: Re: [python-chinese] cjkcodecs
> on
> >> >> > > mandrake, a problem
> >> >> > > >Date: Wed, 17 Mar 2004 10:21:21 +0800
> >> >> > > >
> >> >> > > >Hello Anthony,
> >> >> > > >
> >> >> > > >"'latin-1' codec can't encode" is the
> key!
> >> >> > > >u say::
> >> >> > > >s = "蟒蛇"  # 2 Chinese
> >> characters
> >> AL> for
> >> >> > > >
> >> >> > > >??
> >> >> > > >but we can see it as chinese!
> >> >> > > >so so means in u sys default location is
> >> >> latin-1!!
> >> >> > > >
> >> >> > > >anything encode as latin-1 ??
> >> >> > > >
> >> >> > > >so so poor Mandrake.....
> >> >> > > >
> >> >> > > >
> >> >> > > >
> >> >> > > >=== [ 10:12 ; 04-03-17 ] you wrote:
> >> >> > > >
> >> >> > > >AL> The superuser of the Mandrake system
> in
> >> my
> >> >> lab
> >> >> > > >AL> installed cjkcodecs for python.
> >> >> > > >
> >> >> > > >AL> But there is a problem.
> >> >> > > >
> >> >> > > >AL> I can encode english alphabets like
> 'a',
> >> >> 'b',
> >> >> > > 'c' or
> >> >> > > >AL> 'abc' like so:
> >> >> > > >
> >> >> > > >AL> s = 'abc'
> >> >> > > >AL> s = unicode(s, 'gbk')
> >> >> > > >AL> print s # prints 'abc'
> >> >> > > >
> >> >> > > >AL> but when I tried to encode Chinese
> >> strings,
> >> >> I
> >> >> > > have a
> >> >> > > >AL> problem.
> >> >> > > >
> >> >> > > >AL> For example, to run the following
> >> script:
> >> >> > > >
> >> >> > > >AL> s = "蟒蛇"  # 2 Chinese
> >> AL> characters
> >> >> >for
> >> >> > > >AL> python
> >> >> > > >AL> s = unicode(s, 'gbk')
> >> >> > > >AL> print s
> >> >> > > >
> >> >> > > >AL> I get the following error message.
> >> >> > > >
> >> >> > > >AL> [antony at ancho tmp]$ python snake.py
> >> >> > > >AL> Traceback (most recent call last):
> >> >> > > >AL>   File "snake.py", line 6, in ?
> >> >> > > >AL>     print a
> >> >> > > >AL> UnicodeEncodeError: 'latin-1' codec
> >> can't
> >> >> > > encode
> >> >> > > >AL> characters in position 0-1: ordinal
> not
> >> in
> >> >> > > range(256)
> >> >> > > >AL> [antony at ancho tmp]$
> >> >> > > >
> >> >> > > >AL> What is the problem please?
> >> >> > > >
> >> >> > > >
> >> >> > > >AL> __________________________________
> >> >> > > >AL> Do you Yahoo!?
> >> >> > > >AL> Yahoo! Mail - More reliable, more
> >> storage,
> >> >> less
> >> >> > > spam
> >> >> > > >AL> http://mail.yahoo.com
> >> >> > > >
> >> >> > > >=== === === === === === === === === ===
> >> >> > > >
> >> >> > > >--
> >> >> > > >Best regards,
> >> >> > > >  Zoom.Quiet
> >> >> > > >
> >> >> > > > 
> /=======================================\
> >> >> > > >]Time is unimportant, only life
> important![
> >> >> > > > 
> \=======================================/
> >> >> > > >
> >> >> > >
> >> >_______________________________________________
> >> >> > > >python-chinese list
> >> >> > > >python-chinese at lists.python.cn
> >> >> > >
> >> >>
> >http://python.cn/mailman/listinfo/python-chinese
> >> >> > >
> 
=== message truncated ===>
_______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com


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

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

    你的回复:

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

    Zeuux © 2024

    京ICP备05028076号