Get a list of field values from Python's sqlite3, not tuples representing rows

后端 未结 8 961
我在风中等你
我在风中等你 2020-12-12 17:31

It\'s annoying how Python\'s sqlite3 module always returns a list of tuples! When I am querying a single column, I would prefer to get a plain list.

e.g. when I exec

8条回答
  •  攒了一身酷
    2020-12-12 17:43

    I use the module pandas to deal with table-like content:

    df = pd.DataFrame(cursor.fetchall(), columns=['one','two'])
    

    The list of values for column 'one' is simply reffered as:

    df['one'].values
    

    You even can use you own index for the data referencing:

    df0 = pd.DataFrame.from_records(cursor.fetchall(), columns=['Time','Serie1','Serie2'],index='Time')
    

提交回复
热议问题