Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44464

Re: File Read issue by using module binascii

From Peter Otten <__peter__@web.de>
Subject Re: File Read issue by using module binascii
Date 2013-04-28 09:42 +0200
Organization None
References <9b5795ab-baec-4c0a-a3b4-1075cffc8744@googlegroups.com> <319pn8hgkc0pbj0099heq2nq3h5sv68g7s@4ax.com>
Newsgroups comp.lang.python
Message-ID <mailman.1133.1367134929.3114.python-list@python.org> (permalink)

Show all headers | View raw


Tim Roberts wrote:

> Jimmie He <jimmie.he@gmail.com> wrote:
> 
>>When I run the readbmp on an example.bmp(about 100k),the Shell is become
>>to "No respose",when I change f.read() to f.read(1000),it is ok,could
>>someone tell me the excat reason for this? Thank you in advance!
>>
>>Python Code as below!!
>>
>>import binascii
>>
>>def read_bmp():
>>    f = open('example.bmp','rb')
>>    rawdata = f.read()                       #f.read(1000) is ok
>>    hexstr = binascii.b2a_hex(rawdata)       #Get an HEX number
>>    bsstr = bin (int(hexstr,16))[2:]
> 
> I suspect the root of the problem here is that you don't understand what
> this is actually doing.  You should run this code in the command-line
> interpreter, one line at a time, and print the results.
> 
> The "read" instruction produces a string with 100k bytes.  The b2a_hex
> then
> produces a string with 200k bytes.  Then, int(hexstr,16) takes that
> 200,000 byte hex string and converts it to an integer, roughly equal to 10
> to the
> 240,000 power, a number with some 240,000 decimal digits.  You then
> convert
> that integer to a binary string.  That string will contain 800,000 bytes.
> You then drop the first two characters and print the other 799,998 bytes,
> each of which will be either '0' or '1'.
> 
> I am absolutely, positively convinced that's not what you wanted to do.
> What point is there in printing out the binary equavalent of a bitmap?
> 
> Even if you did, it would be much quicker for you to do the conversion one
> byte at a time, completely skipping the conversion to hex and then the
> creation of a massive multi-precision number.  Example:

Hm, if you fix the long integer arithmetic "problem" you should also attack 
the unbounded memory consumption problem in general ;)

>     f = open('example.bmp','rb')
>     rawdata = f.read()
>     bsstr = []
>     for b in rawdata:
>         bsstr.append( bin(ord(b)) )
>     bsstr = ''.join(bsstr)
> 
> or even:
>     f = open('example.bmp','rb')
>     bsstr = ''.join( bin(ord(b))[2:] for b in f.read() )

Yes, the original is horrible newbie code ;) but that's what you tend to 
write while learning to program -- and python can handle it alright. On the 
other hand, Idle becomes unresponsive when I do

>>> print("a"*10**6)

in its shell. I'm still investigating, but the problem seems to be that it's 
a single line.

>>> print(("a"*100+"\n") * 10**4)

takes under 7 secs. Not as good as konsole (KDE's terminal emulation) which 
finishes in 0.5 secs, but acceptable.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

File Read issue by using module binascii Jimmie He <jimmie.he@gmail.com> - 2013-04-26 20:57 -0700
  Re: File Read issue by using module binascii Jimmie He <jimmie.he@gmail.com> - 2013-04-26 21:22 -0700
    Re: File Read issue by using module binascii Fábio Santos <fabiosantosart@gmail.com> - 2013-04-27 10:56 +0100
      Re: File Read issue by using module binascii Jimmie He <jimmie.he@gmail.com> - 2013-04-27 03:42 -0700
  Re: File Read issue by using module binascii Peter Otten <__peter__@web.de> - 2013-04-27 12:57 +0200
    Re: File Read issue by using module binascii Jimmie He <jimmie.he@gmail.com> - 2013-04-27 04:23 -0700
      Re: File Read issue by using module binascii Fábio Santos <fabiosantosart@gmail.com> - 2013-04-27 12:40 +0100
      Re: File Read issue by using module binascii Peter Otten <__peter__@web.de> - 2013-04-27 14:01 +0200
  Re: File Read issue by using module binascii Jimmie He <jimmie.he@gmail.com> - 2013-04-27 05:46 -0700
  Re: File Read issue by using module binascii Tim Roberts <timr@probo.com> - 2013-04-27 21:34 -0700
    Re: File Read issue by using module binascii Peter Otten <__peter__@web.de> - 2013-04-28 09:42 +0200
    Re: File Read issue by using module binascii jt@toerring.de (Jens Thoms Toerring) - 2013-04-28 12:04 +0000
      Re: File Read issue by using module binascii Jimmie He <jimmie.he@gmail.com> - 2013-04-28 06:32 -0700

csiph-web