Converting a Binary File Gu1 to Csv and Dataframe

I have a binary file with "myfile.gu1" format. It has several rows and columns that from inside it should look like this:

I would like to convert this file to a csv file or a a dataframe so I can then plot the data inside it. The data in the binary file should have around 70 headers and the first column is just the system time. Codes that I tried from Stackoverflow, do convert the data but at the end I get files with many characters that are not defined. This table that I provided, is converted by the measurement PC and that is very slow and cannot be automatized. any ideas for solution is highly appreciated!

I am sure that my code is far from working:

import os
import struct
import csv

# Get the current working directory
cwd = os.getcwd()

# Construct the full path to the binary file
filename = 'myfile.gu1'
filepath = os.path.join(cwd, 'Desktop', filename)

# Open the binary file in read binary mode
with open(filepath, 'rb') as file:
    # Read the binary data and convert it to a list of floats
    data = []
    while True:
        # Read 4 bytes of binary data as a float
        bytes = file.read(4)
        if not bytes:
            break
        float_value = struct.unpack('f', bytes)[0]
        data.append(float_value)
        
# Convert the list of floats to a list of lists with one float per row
rows = [[x] for x in data]

# Construct the full path to the csv file
csv_filename = os.path.splitext(filename)[0] + '.csv'
csv_filepath = os.path.join(cwd, 'Desktop', csv_filename)

# Write the list of lists to a csv file
with open(csv_filepath, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(rows)
4

1 Answer

I'm not sure if this is what you need but it give me an output that is similar to the one you give us as example.

import os
import struct
import pandas as pd

cwd = os.getcwd()

filename = 'Myfille.gu1'
# filepath = os.path.join(cwd, 'Desktop', filename)

with open(filename, 'rb') as file:
    rows = []
    data = []
    cont = 0
    while True:
        bytes = file.read(2)
        if cont == 111:
          rows.append(data)
          data = []
          cont = 0
        if not bytes:
          break
        try:
          bytes = bytes + b'\x00\x00'
          float_value = struct.unpack('I', bytes)[0]
          data.append(float_value)
          cont+=1
        except:
          print(bytes)
        
rows = pd.DataFrame(rows)
print(rows)

rows.to_csv("Myfille.csv")

Output:

b'\x02\x00\x00'
         0      1    2    3    4    5    6    7    8     9    ...    101  102  \
0      17216  39225  565  567  569  568  567  457  457   458  ...  14336    2   
1      17216  39230  566  566  568  567  569  456  459   458  ...  14336    2   
2      17216  40000  566  568  569  568  567  457  458   458  ...  14336    2   
3      17216  41000  567  569  570  568  571  454  456   457  ...  14336    2   
4      17216  42000  565  566  568  567  567  456  458   458  ...  14336    2   
...      ...    ...  ...  ...  ...  ...  ...  ...  ...   ...  ...    ...  ...   
43355  18496  57815  258  566  567  569  568  569  516  1024  ...      0    0   
43356  18496  57820  258  566  567  569  568  569  516  1024  ...      0    0   
43357  18496  57825  258  566  567  569  568  569  516  1024  ...      0    0   
43358  18496  57830  258  566  567  569  568  569  516  1024  ...      0    0   
43359  18496  57835  258  566  567  569  568  569  516  1024  ...      0    0   

       103  104  105    106  107    108   109  110  
0        0    0    0      0    0  57602  6568  527  
1        0    0    0      0    0  57602  6568  527  
2        0    0    0      0    0  58114  6568  527  
3        0    0    0      0    0  58114  6568  527  
4        0    0    0      0    0  58370  6568  527  
...    ...  ...  ...    ...  ...    ...   ...  ...  
43355    0    0    0  13824    0      0  5384  342  
43356    0    0    0  13824    0      0  5384  342  
43357    0    0    0  13824    0      0  5384  342  
43358    0    0    0  13824    0      0  5384  342  
43359    0    0    0  13824    0      0  5384  342  

[43360 rows x 111 columns]

I hope it helps

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest