Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > chr / ord

Reply
Thread Tools

chr / ord

 
 
Sean McIlroy
Guest
Posts: n/a
 
      11-03-2009
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]))
 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      11-03-2009
On Mon, Nov 2, 2009 at 11:30 PM, Sean McIlroy <(E-Mail Removed)> 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
>

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-03-2009
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
 
Reply With Quote
 
Sean McIlroy
Guest
Posts: n/a
 
      11-03-2009

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
ascii ?c "c"[0] "c".ord.chr question Stu Ruby 1 12-03-2010 09:19 PM
ord function problem from newbie David.J.Anderson66@gmail.com Python 3 03-18-2008 04:18 AM
Lost XP ord Dog Computer Support 1 01-28-2004 10:26 PM
Lost XP ord Dog Computer Support 0 01-28-2004 08:58 PM
Prevent 'Chr(13) + Chr(10)' from Being Executed in String crjunk ASP .Net 2 08-08-2003 04:14 PM



Advertisments