I am trying to parse the following data file. The file is a snippet from the original file which is much larger, but has same structure.
0 0.0059354815313768 0.000109666861931809 4.67297178729149e-05 0.000160593629759828
1e-07 0.0059354815313768 0.000109666861931809 4.67297178729149e-05 0.000160593629759828
1.20226443461741e-07 0.00593548153136993 0.000134002335569027 4.67297178728227e-05 0.000201020108334994
1.31825673855641e-07 0.00593548153136543 0.000147957965791881 4.67297178727586e-05 0.000224203424726248
1.44543977074593e-07 0.00593548153135997 0.000163260010030845 4.67297178726794e-05 0.000249623425870511
1.58489319246111e-07 0.00593548153135335 0.000180038367935316 4.67297178725815e-05 0.000277495902647069
1.58489319fcdsdds-07 0.00593548153135335 0.000180038367935316 4.67297178725815e-05 0.000277495902647069
In the above data file its a 22 matrix, but can be a nn matrix. The elements are separated by \t
. In case of a 2*2 matrix each row will have 5 elements (1st frequency and the other 2 and 2 elements make 1 value).
For example:
0 0.0059354815313768 0.000109666861931809 4.67297178729149e-05 0.000160593629759828
0
is frequency. 0.0059354815313768 0.000109666861931809
is element 1 (but they are two different values) and 4.67297178729149e-05 0.000160593629759828
is element 2 (similarly they are also two different values).
The matrixes can be for any number of frequencies. I do not know the frequencies in advance, but I do know the matrix size (i.e. its a 2*2 matrix) in advance.
The was I implementing it was:
- Split the items by
\t
and add them sequentially to a list. - Run an outer loop until there are elements in the list.
- Run an inner loop until matrix size + 1 (for frequency). So in this example (2*2+1)
- The 0th element in the inner loop will be frequency. Append the frequency to a separated list and remove it from the original list.
- Build a map (key is frequency and value is the matrix). Or a python object.
- Keep removing the items from the original list.
Below is my code to get the frequency:
if __name__=="__main__":
with open("temp.txt", "r") as file:
newline_break = ""
list_test = []
for readline in file:
line_strip = readline.split('\t')
for ll in line_strip:
if ll != '' and ll != ' ':
list_test.append(ll.strip())
freq = []
length = len(list_test)
while length > 0:
freq.append(list_test[0])
for i in range(0, 6, 1):
#print('poping', i)
if len(list_test) > 0:
list_test.pop()
print('list 2 size', len(list_test))
if len(list_test) > 0:
print('list 2 item', list_test[0])
length = len(list_test)
print(len(list_test))
print('Freq is: ',freq)
The code does remove the item, but it always prints "0".
Freq is: ['0', '0', '0', '0', '0', '0', '0']