How to turn a string into a list of integers?

Kurt Mueller kurt.alfred.mueller at gmail.com
Fri Sep 5 13:56:16 EDT 2014


Am 05.09.2014 um 10:42 schrieb cl at isbd.net:

> Joshua Landau <joshua at landau.ws> wrote:
>> On 3 September 2014 15:48,  <cl at isbd.net> wrote:
>>> Peter Otten <__peter__ at web.de> wrote:
>>>>>>> [ord(c) for c in "This is a string"]
>>>> [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]
>>>> 
>>>> There are other ways, but you have to describe the use case and your Python
>>>> version for us to recommend the most appropriate.
>>>> 
>>> That looks OK to me.  It's just for outputting a string to the block
>>> write command in python-smbus which expects an integer array.
>> 
>> Just be careful about Unicode characters.
> 
> I have to avoid them completely because I'm sending the string to a
> character LCD with a limited 8-bit only character set.


Could someone please explain the following behavior to me:
Python 2.7.7, MacOS 10.9 Mavericks

>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> [ord(c) for c in 'AÄ']
[65, 195, 132]
>>> [ord(c) for c in u'AÄ']
[65, 196]

My obviously wrong understanding:
‚AÄ‘ in ‚ascii‘ are two characters
     one with ord A=65 and
     one with ord Ä=196 ISO8859-1 <depends on code table>
     —-> why [65, 195, 132]
u’AÄ’ is an Unicode string
     —-> why [65, 196]

It is just the other way round as I would expect.



Thank you
-- 
Kurt Mueller, kurt.alfred.mueller at gmail.com




More information about the Python-list mailing list