chr / ord

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Nov 3 00:10:23 EST 2009


On Mon, Nov 2, 2009 at 11:30 PM, Sean McIlroy <sean_mcilroy at yahoo.com> wrote:
> hello
>
> how do i say "chr" and "ord" in the new python? the functions below
> (which work in 2.6.6) show what i'm trying to do. thanks if you can
> help.
>
> def readbytes(filepath):
>    return [ord(x) for x in open(filepath,'rb').read()]
>
Ord should still work the way you expect.

> def writebytes(numbers,filepath):
>    open(filepath,'wb').write(''.join([chr(x) for x in numbers]))

I haven't played around with python 3 that much, but I believe that
the bytes constructor can take an iterable of ints. So you should be
able to do
open(filepath,'wb').write(bytes(numbers))

> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list