Python Forum
Parse Binary Data File and convert Epoch Time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse Binary Data File and convert Epoch Time
#1
Hello All!

I am trying to extract and convert data from a binary data file. Each data entry follows this order:
  • sample time in seconds since unix epoch, 4 bytes
  • 2 byte integer
  • 2 byte integer
  • 2 byte integer
  • 2 byte integer

All values are integers

Below is the code I'm using based off a different binary data extraction project. It exports the correct number of data entries but the values do not make sense. I know I'm not parsing the binary file correctly but I;m not sure how to do the date conversion. Thanks!

import math, struct, time field_names = ('time1', 'vol_conc', 'part_size', \ 'opt_trans', 'laser_ref') def decode(data, offset=0): '''Decipher a lisst datagram from binary''' # Offset now tells it how far to start with open('li0002a', 'rb') as data: values = struct.unpack('i4h', data.read(12)) # Create a dictionary for all the values lisst_values = dict(zip (field_names, values)) return lisst_values def lisst_print(lisst_values): 'Print out all the values of a lisst dictionary' print 'results:' for key in lisst_values: print ' ', key, lisst_values[key] datagram_size = 12 # 2*6 bytes per datagram def num_datagrams(data): #How many packets are in data' # Make sure we have an even number of datagrams #assert (len(data) % datagram_size == 0) return len(data) / datagram_size def get_offset(datagram_number): 'Calculate the starting offset of a datagram' return datagram_number * datagram_size def main(): lisst_file = open('li0002a') lisst_data = lisst_file.read() print 'Number of datagrams:', num_datagrams(lisst_data) print 'Datagram Number, Time, Volume Concentration, Particle Size,\ Optical Transmission, Laser Reference ' for datagram_index in range( num_datagrams(lisst_data) ): offset = get_offset(datagram_index) datagram = decode(lisst_data,offset) print datagram_index, datagram['time1'], datagram['vol_conc'], datagram['part_size'], \ datagram['opt_trans'], datagram['laser_ref'] if __name__=='__main__': main()
Reply
#2
pure binary as in a block of memory is arranged in machine addressing order, big or little endian, so your 4 byte value.
so you may have to do some transformations, Here's a good doc for that: https://wiki.python.org/moin/BitManipulation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 3,093 Jan-02-2025, 02:09 AM
Last Post: lyly19
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 2,843 Dec-19-2024, 11:57 AM
Last Post: snippsat
Question Convert UTC now() to local time to compare to a strptime() Calab 2 2,920 Apr-29-2024, 07:24 PM
Last Post: deanhystad
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 16,291 Feb-29-2024, 12:30 AM
Last Post: Winfried
  parse json field from csv file lebossejames 4 2,956 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 3,942 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  parse/read from file seperated by dots giovanne 5 3,215 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  How do I read and write a binary file in Python? blackears 6 35,091 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Script to convert Json to CSV file chvsnarayana 8 6,297 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  [SOLVED] Epoch timestamp without milliseconds? Winfried 5 8,867 Jan-27-2023, 04:35 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.