[Tutor] How to iterate through unicode string.

zhang allen acallen99 at gmail.com
Fri Sep 4 15:14:12 CEST 2009


Thank you very much. :)

it works now.

2009/9/4 Christian Witts <cwitts at compuscan.co.za>

> If it's not you can put an encoding line @ the top of your script like
> # -*- encoding: utf-8 -*-
>
> David Stanek wrote:
>
>> Is the encoding of your file set?
>>
>>
>> On 9/4/09, zhang allen <acallen99 at gmail.com> wrote:
>>
>>
>>> Hi Christian,
>>>
>>> Thanks for your tip.
>>>
>>> But it seems to me still not working again.
>>>
>>> i wirte this python code
>>>
>>> k = []
>>> s = u'Büro'
>>> for c in s:
>>>     k.append(c)
>>> print k
>>>
>>> k=[u'B', u'\xa8', u'\xb9', u'r', u'o']
>>>
>>> i still have 5 different chars.
>>>
>>>
>>>
>>>
>>>
>>> 2009/9/4 Christian Witts <cwitts at compuscan.co.za>
>>>
>>>
>>>
>>>> zhang allen wrote:
>>>>
>>>>
>>>>
>>>>> Hi All,
>>>>>
>>>>> Say i have unicode string * Büro*.
>>>>> i want to iterate this string .
>>>>>
>>>>> i write this python code which doesn't work.
>>>>>
>>>>> s ='Büro'
>>>>> for ch in s:
>>>>> print ch
>>>>>
>>>>> it seems *Büro* has 5 chars. *ü *consists of 2 bytes.
>>>>>
>>>>> so does someone has any ideas?
>>>>>
>>>>> how to iterate this string, so i can hava 4 chars, like "B, ü, r, o ".?
>>>>>
>>>>> Thanks in advance.
>>>>>
>>>>>
>>>>> --
>>>>> Best Regards,
>>>>> Allen
>>>>>
>>>>> ------------------------------------------------------------------------
>>>>>
>>>>> _______________________________________________
>>>>> Tutor maillist  -  Tutor at python.org
>>>>> To unsubscribe or change subscription options:
>>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> s = 'Büro'
>>>>>>> for ch in s:
>>>>>>>
>>>>>>>
>>>>>> ... print ch
>>>> ...
>>>> B
>>>>
>>>>>>>> r
>>>> o
>>>>
>>>> You need to make it a unicode string.
>>>>
>>>> s = u'Büro'
>>>>
>>>>
>>>>>  for ch in s:
>>>>>>>
>>>>>>>
>>>>>> ... print ch
>>>> ...
>>>> B
>>>> ü
>>>> r
>>>> o
>>>>
>>>> --
>>>> Kind Regards,
>>>> Christian Witts
>>>>
>>>>
>>>>
>>>>
>>>>
>>> --
>>> Best Regards,
>>> Allen
>>>
>>>
>>>
>>
>>
>>
>
> --
> Kind Regards,
> Christian Witts
>
>
>


-- 
Best Regards,
Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090904/48687d89/attachment.htm>


More information about the Tutor mailing list