Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read binary from file
#1
I have some trouble reading binary file



with open('myfile', 'rb') as fl:
    b = fl.read( 1 )
    for x in range( 0,4):
         tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1
The interpreter says
TypeError: unsupported operand type(s) for &: 'int' and 'str'

Also
b = int( fl.read( 1 ), 10 )
and
b = int( fl.read( 1 ) )
for both says
ValueError: invalid literal for int() with base 10: 'i'

i dont know what im doing wrong
Reply
#2
Please post entire error traceback verbatim
supple sample file if possible, or where it can be downloaded
b will read 1 byte, starting at byte number 1 in file
You don't show tuppleCounter
is this what you wanted?

You don't need to supply number base to int if decimal
If I try with random binary file:
>>> with open('image_002.jpg', 'rb') as fp:
...     b = fp.read(1)
... 
>>> b
b'\xff'
>>> for x in range( 0,4):
...     tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'tuppleCounter' is not defined
Reply
#3
if __name__ == "__main__":

    tuppleCounter = [ 0, 0, 0 ,0 ]

    with open('myfile', 'rb') as fl:
        b = fl.read( 1 )

        for x in range( 0,4):
            tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1

    print tuppleCounter;

nextor@leon:~/Projects/tmp/pypress$ python test.py
Traceback (most recent call last):
File "test.py", line 10, in <module>
tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1
TypeError: unsupported operand type(s) for &: 'int' and 'str'
nextor@leon:~/Projects/tmp/pypress$
Reply
#4
Are you using PYTHON 2 ?????
Reply
#5
(Nov-27-2018, 08:45 PM)Gribouillis Wrote: Are you using PYTHON 2 ?????
it says Python 2.7.15+
And with Python 3.6.7rc1 it says the same
Reply
#6
Python 2 is almost dead (still supported during one year), you should be using python 3.5 or 3.6 or 3.7.
You need to convert the char you read to an int. In python 2, do
b = fl.read(1)
b = ord(b)
In python 3, use
b = fl.read(1)
b = b[0]
Reply
#7
It works, Thanks Gribouillis
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,855 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,408 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,092 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,085 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,240 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,398 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,185 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,570 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,903 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,053 Jan-21-2023, 11:33 PM
Last Post: SamLiu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020