0

In order to persist some data, I use mysql as my database. I needed to store arrays of floats, and I didn't manage to do it properly, so I decided to store them as strings, thinking I'll just restore them to be a float array once I read them from the database.

So, two questions: 1. is there a better way to store those? 2. how do I convert a string array such as:

Outp = '[0.0, 0.22532551050405566, 0.32132351221384514, 8.329281012428868e-18, 0.012183960426492089, 0.14915942181052608, 0.0, 0.2920075950450811]'

I tried multiple manipulations with numpy objects, lists and floats. With no success, they all seem to address the variable as a single item and not as a list.

looking for a good way either to save it into the DB in a way that it is float when I read it from the DB or, a way to convert it to float.

  • Doesn't `ast.literal_eval()` work? – Barmar Jun 28 '19 at 19:19
  • 1
    You should normalize your database schema. Create a table with two columns: a foreign key to the original table, and a `FLOAT` column containing the number. – Barmar Jun 28 '19 at 19:20
  • Are you sure I can store a list of floats in a float field? – Daniel Magal Jun 28 '19 at 19:26
  • You're just storing one float in each field. The list comes from having multiple rows with the same foreign key. – Barmar Jun 28 '19 at 19:28
  • See https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Barmar Jun 28 '19 at 19:31
  • @Barmar, it worked. I never used ast.literal_eval() before. Thanks!! – Daniel Magal Jun 28 '19 at 19:39
  • @Barmar, I'm fully aware to the need to normalize the DB, but I'm storing so much numerical data (2D, 3D Matrixes that it will become ridiculous) I'm not indexing it, and not updating it. Not sure Relational DB is the best solution for my application. – Daniel Magal Jun 28 '19 at 19:51

1 Answers1

0

You can use ast.literal_eval()

import ast
float_list = ast.literal_eval(Outp)

However, it would be best to normalize your table schema, as comma-separated lists are difficult to work with in databases. See Is storing a delimited list in a database column really that bad?

Barmar
  • 741,623
  • 53
  • 500
  • 612