converting python list of strings to their type

后端 未结 7 632
旧时难觅i
旧时难觅i 2020-12-05 19:57

given a list of python strings, how can I automatically convert them to their correct type? Meaning, if I have:

[\"hello\", \"3\", \"3.64\", \"-1\"]
<         


        
相关标签:
7条回答
  • 2020-12-05 20:43

    This is not really an answer, but I wanted to point out how important this can be when you have a database of parameters with schema ID, PAR, VAL. For instance:

    ID  PAR      VAL
    001 velocity '123.45'
    001 name     'my_name'
    001 date     '18-dec-1978'
    

    This schema is appropriate when you don't know how many parameters you need to store for a certain ID. The disadvantage is precisely that the values in VAL are all strings, and need to be converted to the correct data type on demand. You can do this by adding a fourth column to the schema, called TYPE, or you can use any of the approaches proposed thus far.

    Good question!

    PS. The database schema is related to one of my previous questions.

    0 讨论(0)
提交回复
热议问题