file_name=r'/home/user/python/dataset/model_data5.txt'
file=open(file_name,'r')
data = file.read()#read the whole binary file to string
file.close()
data =data.replace('\n','').replace('\t','')
# data = data.expandtabs(0)可以将tab消去
# print(data)
file_name1 = "weight1.dat"
file1 = open(file_name1, 'w')
num=0
weight_len=9
i=0
# a='00001101'
# int_10=int(a,2)
# print(int_10)
# aaa=struct.pack("string", a)
# print(int(a))
while num<=len(data)-weight_len:
a=data[num:num+weight_len]
print(a)
data_bin=int(a,2)
print(data_bin)
# b=hex(int(a))[2:].zfill(9)
# file1.write(hex(b)[2:].zfill(4))
# print(b)
file1.write('@')
file1.write(hex(i)[2:].zfill(4))
file1.write('\n')
file1.write(hex(int(data_bin))[2:].zfill(8)) # zfill(8) stardard hex
file1.write('\n')
i+=1
num += 9
file1.close()
主要是按这个格式自行修改,int(a,2),将一个字符串以二进制的格式转换为10进制。struck这个函数库也有这个功能。