voglio leggere un file byte per byte e verificare se è impostato l'ultimo bit di ogni byte:Lettura e interpretazione dei dati da un file binario in Python
#!/usr/bin/python
def main():
fh = open('/tmp/test.txt', 'rb')
try:
byte = fh.read(1)
while byte != "":
if (int(byte,16) & 0x01) is 0x01:
print 1
else:
print 0
byte = fh.read(1)
finally:
fh.close
fh.close()
if __name__ == "__main__":
main()
L'errore che ottengo è:
Traceback (most recent call last):
File "./mini_01.py", line 21, in <module>
main()
File "./mini_01.py", line 10, in main
if (int(byte,16) & 0x01) is 0x01:
ValueError: invalid literal for int() with base 16: '\xaf'
Qualcuno un'idea? Non ho avuto successo usando i moduli struct e binascii.
Leggi http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers prima che venga * this * funzionante e apri un duplicato di * that * question;) – delnan