![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|