Searching/reading binary data in Python

前端 未结 8 1842
情歌与酒
情歌与酒 2020-12-25 12:15

I\'m reading in a binary file (a jpg in this case), and need to find some values in that file. For those interested, the binary file is a jpg and I\'m attempting to pick out

相关标签:
8条回答
  • 2020-12-25 13:16

    You could actually load the file into a string and search that string for the byte sequence 0xffc0 using the str.find() method. It works for any byte sequence.

    The code to do this depends on a couple things. If you open the file in binary mode and you're using Python 3 (both of which are probably best practice for this scenario), you'll need to search for a byte string (as opposed to a character string), which means you have to prefix the string with b.

    with open(filename, 'rb') as f:
        s = f.read()
    s.find(b'\xff\xc0')
    

    If you open the file in text mode in Python 3, you'd have to search for a character string:

    with open(filename, 'r') as f:
        s = f.read()
    s.find('\xff\xc0')
    

    though there's no particular reason to do this. It doesn't get you any advantage over the previous way, and if you're on a platform that treats binary files and text files differently (e.g. Windows), there is a chance this will cause problems.

    Python 2 doesn't make the distinction between byte strings and character strings, so if you're using that version, it doesn't matter whether you include or exclude the b in b'\xff\xc0'. And if your platform treats binary files and text files identically (e.g. Mac or Linux), it doesn't matter whether you use 'r' or 'rb' as the file mode either. But I'd still recommend using something like the first code sample above just for forward compatibility - in case you ever do switch to Python 3, it's one less thing to fix.

    0 讨论(0)
  • 2020-12-25 13:18

    Well, obviously there is PIL The Image module has size as an attribute. If you are wanting to get the size exactly how you suggest and without loading the file you are going to have to go through it line by line. Not the nicest way to do it but it would work.

    0 讨论(0)
提交回复
热议问题