chr / ord

View: New views
5 Messages — Rating Filter:   Alert me  

chr / ord

by Sean McIlroy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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]))
--
http://mail.python.org/mailman/listinfo/python-list

Re: chr / ord

by Ben Finney-10 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sean McIlroy <sean_mcilroy@...> writes:

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

By “the new python”, what do you mean?

* Python 2.6.4, the newest released version.
* Python 2.7, a currently in-development version.
* Python 3.1, another new release (but not the latest).
* Python 3.2, a currently in-development version.
* Something else.

> the functions below (which work in 2.6.6)

I think we'll need you to have a closer look at version numbers; there
is no Python 2.6.6.

> show what i'm trying to do.

Unfortunately, they leave us guessing as to what you what to do, and
what you expect the result to be. I prefer not to guess.

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

Could you please show an example of a use of these functions —
preferably a complete, minimal example that anyone can run in the
declared version of Python — and what you would expect the output to be?

--
 \       “Remorse: Regret that one waited so long to do it.” —Henry L. |
  `\                                                           Mencken |
_o__)                                                                  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list

Re: chr / ord

by Benjamin Kaplan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Nov 2, 2009 at 11:30 PM, Sean McIlroy <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
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: chr / ord

by Steven D'Aprano-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: chr / ord

by Sean McIlroy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

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