The best way to store a python list to a database?

前端 未结 4 1289
一生所求
一生所求 2021-02-06 11:50

What would be the best way of storing a python list of numbers (such as [4, 7, 10, 39, 91]) to a database? I am using the Pyramid framework with SQLAlchemy to communicate to a d

4条回答
  •  时光说笑
    2021-02-06 12:19

    You can use json to save your arrays in db as stings:

    In [1]: import json
    
    In [2]: json.dumps([1, 2, 3])
    Out[2]: '[1, 2, 3]'
    
    In [3]: json.loads(json.dumps([1, 2, 3]))
    Out[3]: [1, 2, 3]
    

    also consider using cjson for better performances and anyjson for nice fallbacks.

提交回复
热议问题