To import the data contained into the file my_file.txt
that have the form:
Label[0] = 0.980252
Label[1] = -nan
Label[2] = -nan
Label[3] = -nan
Label[4] = 0.664706
Label[5] = -nan
Label[6] = -nan
Label[7] = -nan
Label[8] = -nan
Label[9] = -nan
Label[10] = -nan
Label[11] = 0.800183
Label[12] = -nan
Label[13] = -nan
Label[14] = -nan
Label[15] = 0
Mean Data = 15
I wrote the following code:
import numpy as np
with open('myfile.txt', 'r') as file_txt_original:
data = file_txt_original.read()
data = data.replace('Mean data', '-1')
data = data.replace('Label[', '')
data = data.replace(']', '')
data = data.replace(' = ', ', ')
file_txt_original.close()
with open('new_file.txt', 'w') as file_txt_copy:
file_txt_copy.write(data)
file_txt_copy.close()
my_array = np.loadtxt('new_file.txt', delimiter=',')
It works but this to me seems still quite an tricky solution... Any suggestion to improve this code without doing so many replacement or without saving an additional structure?