Converting list to float array

0

I'm working on a data import, which are in the format, as in the example:

[-2.814285863362681, -2.814158589901394, -2.8123844084634873]
[-2.874285863362681, -2.054158589901394, -2.6523844084634873]

.
.
.

I used this code to import, already removing '\ n' with strip ()

with open(coeffs_csv) as inp:
        signals_coeffs_cluster = inp.readlines()
        signals_coeffs_cluster = [x.strip() for x in signals_coeffs_cluster]

But I need this data in float and array format, so I worked on the code for just the second line:

signal_1.append(signals_coeffs_cluster[1])

signal_1 = [k.replace('[', '').replace(']', '') for k in signal_1]

And this returns me a size 1, that is, everything is concatenated within signal_1, I can not, for example make signal_1 [2], since there is no position.

How do I extract a line from the input file and separate them as individual float array?

array_1 = [-2.814285863362681, -2.814158589901394, -2.8123844084634873] 
array_2 = [-2.874285863362681, -2.054158589901394, -2.6523844084634873]

And so on?

EDIT:

I use this code to export:

coeffs = cA3.tolist()

    with open(coeffs_csv, "a") as output:
        json.dump(coeffs, output)
        output.write('\n')

Thank you

    
asked by anonymous 30.04.2018 / 16:37

1 answer

0

You can use eval to transform a string into its Python equivalent:

minha_lista_str = '[1, 2, "a", 2.3]'
minha_lista = eval(minha_lista_str)
print(minha_lista, type(minha_lista))
# [1, 2, 'a', 2.3] <class 'list'>

Then your code stays:

with open(coeffs_csv) as inp:
        signals_coeffs_cluster = [eval(x) for x in inp.readlines()]
    
30.04.2018 / 22:59