Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

chr / ord

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


sean_mcilroy at yahoo

Nov 2, 2009, 8:30 PM

Post #1 of 5 (704 views)
Permalink
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]))
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 2, 2009, 9:10 PM

Post #2 of 5 (686 views)
Permalink
Re: chr / ord [In reply to]

Sean McIlroy <sean_mcilroy [at] yahoo> 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


benjamin.kaplan at case

Nov 2, 2009, 9:10 PM

Post #3 of 5 (681 views)
Permalink
Re: chr / ord [In reply to]

On Mon, Nov 2, 2009 at 11:30 PM, Sean McIlroy <sean_mcilroy [at] yahoo> 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


steve at REMOVE-THIS-cybersource

Nov 2, 2009, 9:24 PM

Post #4 of 5 (670 views)
Permalink
Re: chr / ord [In reply to]

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


sean_mcilroy at yahoo

Nov 3, 2009, 11:40 AM

Post #5 of 5 (656 views)
Permalink
Re: chr / ord [In reply to]

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.