[Tutor] surprising len() results ???

Tom Cloyd tomcloyd at bestmindhealth.com
Tue Aug 9 22:19:13 CEST 2005


Michael and List,

You suggestion (see foot of email trail, below) is a bit over my head. As  
I stated, I removed ActivePython 2.4.1 installed Python 2.4.1 from  
python.org, and now my program doesn't work, where before it did. I don't  
understand this. When isn't Python 2.4.1 == Python 2.4.1? Is this a Python  
level problem, or something in my script?

At the head of my script I explicitly set coding to utf-8, but that't not  
new.

Do I need to change a parameter somewhere? More to the point, what do I  
have to do to get going again. I really don't know.

Thanks in advance for any help!

Tom C.

On Tue, 09 Aug 2005 03:33:11 -0700, Michael Janssen <mi.janssen at gmail.com>  
wrote:

> On 8/9/05, Tom Cloyd <tomcloyd at bestmindhealth.com> wrote:
>
>> print len('()ÄäÀÁàáÇçÈÉèéÌÍìíÑñÒÓòóÙÚúù')
>> print len('--AaAAaaCcEEeeIIiiNnOOooUUuu')
>>
>> the result:
>>
>> 54
>> 28
>>
>> I'm completely mystified by this. All of it. None of it makes sense.  
>> This
>> program was working fine. Now it doesn't. And those two parameter string
>> are plainly the same length, but Python doesn't think so.
>
> on my computer (python 2.3):
>>>> s = 'ä' # a-umlaut
>>>> len(s)
> 1
>>>> len(s.decode('latin-1')) # prepare for utf-8 encoding
> 1
>>>> len(s.decode('latin-1').encode('utf-8'))
> 2
>>>> len('a'.decode('latin-1').encode('utf-8'))
> 1
>
> seems like len returns the number of bytes and some encodings uses
> more than one byte for certain chars. You can proberbly decode your
> strings from utf-8 (or whatever encoding you use (and perhaps encode
> it back into a one-char-one-byte encoding [on my system the decoded
> (unicode) string is just fine]).
>
> regards
> Michael
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



-- 

======================================================
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com / tc at bestmindhealth.com >>
======================================================

Using Opera's revolutionary e-mail client (program):  
http://www.opera.com/mail/


More information about the Tutor mailing list