Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - chr / ord

 
Thread Tools Search this Thread
Old 11-03-2009, 04:30 AM   #1
Default chr / ord


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
  Reply With Quote
Old 11-03-2009, 05:10 AM   #2
Benjamin Kaplan
 
Posts: n/a
Default Re: chr / ord
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
  Reply With Quote
Old 11-03-2009, 05:24 AM   #3
Steven D'Aprano
 
Posts: n/a
Default Re: chr / ord
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
  Reply With Quote
Old 11-03-2009, 07:40 PM   #4
Sean McIlroy
 
Posts: n/a
Default Re: chr / ord

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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46