chr / ord

Discussion in 'Python' started by Sean McIlroy, Nov 3, 2009.

  1. Sean McIlroy

    Sean McIlroy Guest

    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()]

    def writebytes(numbers,filepath):
    open(filepath,'wb').write(''.join([chr(x) for x in numbers]))
    Sean McIlroy, Nov 3, 2009
    #1
    1. Advertising

  2. On Mon, Nov 2, 2009 at 11:30 PM, Sean McIlroy <> 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
    >
    Benjamin Kaplan, Nov 3, 2009
    #2
    1. Advertising

  3. On Mon, 02 Nov 2009 20:30:00 -0800, Sean McIlroy wrote:

    > hello
    >
    > how do i say "chr" and "ord" in the new python?


    "chr" and "ord".



    > the functions below (which work in 2.6.6)


    Can I borrow your time machine, there's some lottery numbers I want to
    get.

    There is no Python 2.6.6. The latest version of 2.6 is 2.6.4.


    > 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()]
    >
    > def writebytes(numbers,filepath):
    > open(filepath,'wb').write(''.join([chr(x) for x in numbers]))



    Have you tried them in "the new Python" (whatever that is...)? What do
    they do that isn't what you expect?


    --
    Steven
    Steven D'Aprano, Nov 3, 2009
    #3
  4. Sean McIlroy

    Sean McIlroy Guest

    thanks. that did the trick. in case anyone else is in the same boat as
    myself, here are the relevant correspondences:

    string <-> [int] bytes <-> [int]
    ---------------
    --------------

    lambda string: [ord(x) for x in string] list
    lambda ints: ''.join([chr(x) for x in ints]) bytes
    Sean McIlroy, Nov 3, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crjunk
    Replies:
    2
    Views:
    16,816
    crjunk
    Aug 8, 2003
  2. Angus Comber
    Replies:
    7
    Views:
    1,143
    Richard Heathfield
    Feb 5, 2004
  3. Replies:
    3
    Views:
    250
    7stud
    Mar 18, 2008
  4. Replies:
    20
    Views:
    847
    Nobody
    Aug 30, 2009
  5. Stu
    Replies:
    1
    Views:
    119
    Ryan Davis
    Dec 3, 2010
Loading...

Share This Page