parse binary file in python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Røsdal

    parse binary file in python?

    Hello,
    I want to parse a binary file in python. Does
    python have some built in methods for doing this easily?
    Any links to example code would be nice..
    Thanks

    Andreas R.
  • Martin v. Löwis

    #2
    Re: parse binary file in python?

    Andreas Røsdal wrote:[color=blue]
    > I want to parse a binary file in python. Does
    > python have some built in methods for doing this easily?
    > Any links to example code would be nice..[/color]

    Depends on the kind of parsing you want to do. Python
    can naturally represent binary files in string objects,
    e.g.

    f = open("binaryfil e.bin", "rb") # notice the b for binary mode
    data = f.read()
    f.close()

    You can then look at the individual bytes by index. People
    often use the struct and array modules to simplify processing.
    Whether or not you would call this "parsing", I don't know
    (for me, "parsing" implies presence of a context-free or
    regular grammar of some kind).

    Regards,
    Martin

    Comment

    • Jeff Epler

      #3
      Re: parse binary file in python?

      Python's primary tool for this is the "struct" module. See the stanard
      library documentation for more info.

      Also, remember always to open files in binary mode ("rb") so that people
      running on windows don't get bizarre failures.

      Jeff

      Comment

      • Miki Tebeka

        #4
        Re: parse binary file in python?

        Hello Andreas,
        [color=blue]
        > I want to parse a binary file in python. Does
        > python have some built in methods for doing this easily?[/color]
        Apart from struct and array module already suggested if you want
        specific structures I recommend reading them in C and exposing the
        interface using swig.

        This way you can access structure members by name and not by offset.

        HTH.
        Miki

        Comment

        • Andreas Røsdal

          #5
          Re: parse binary file in python?

          On Sun, 18 Jan 2004, "Martin v. Löwis" wrote:[color=blue]
          > Andreas Røsdal wrote:[color=green]
          > > I want to parse a binary file in python. Does
          > > python have some built in methods for doing this easily?
          > > Any links to example code would be nice..[/color]
          >
          > Depends on the kind of parsing you want to do. Python
          > can naturally represent binary files in string objects,
          > e.g.
          >
          > f = open("binaryfil e.bin", "rb") # notice the b for binary mode
          > data = f.read()
          > f.close()
          >
          > You can then look at the individual bytes by index.[/color]

          Thanks. Just wanting to know if I understood your answer correctly;
          will the data variable above be an array of binary data,
          eg. data[220] == 0, and data[221] == 1 etc?

          Andreas Røsdal

          Comment

          • Michael Hudson

            #6
            Re: parse binary file in python?

            Andreas Røsdal <andrearo@stud. ntnu.no> writes:
            [color=blue]
            > On Sun, 18 Jan 2004, "Martin v. Löwis" wrote:[color=green]
            > > Andreas Røsdal wrote:[color=darkred]
            > > > I want to parse a binary file in python. Does
            > > > python have some built in methods for doing this easily?
            > > > Any links to example code would be nice..[/color]
            > >
            > > Depends on the kind of parsing you want to do. Python
            > > can naturally represent binary files in string objects,
            > > e.g.
            > >
            > > f = open("binaryfil e.bin", "rb") # notice the b for binary mode
            > > data = f.read()
            > > f.close()
            > >
            > > You can then look at the individual bytes by index.[/color]
            >
            > Thanks. Just wanting to know if I understood your answer correctly;
            > will the data variable above be an array of binary data,
            > eg. data[220] == 0, and data[221] == 1 etc?[/color]

            No, it's a string, but you can get what you want by using the `ord'
            builtin function or the array module or the struct module (or probably
            various other ways).

            Cheers,
            mwh

            --
            To summarise the summary of the summary:- people are a problem.
            -- The Hitch-Hikers Guide to the Galaxy, Episode 12

            Comment

            Working...