我有一个“myfile.gu1”格式的二进制文件。它有几行和几列,从里面看应该是这样的:
我想将此文件转换为 csv 文件或数据框,以便我可以绘制其中的数据。二进制文件中的数据应该有大约 70 个标题,第一列只是系统时间。我从 Stackoverflow 尝试的代码确实转换了数据,但最后我得到的文件包含许多未定义的字符。我提供的这张表是由测量 PC 转换的,速度很慢,无法自动化。非常感谢任何解决方案的想法!
我确定我的代码无法正常工作:
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)
我不确定这是否是您需要的,但它给我的输出类似于您给我们举的例子。
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")
输出:
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]
希望对你有帮助