PyMySQL and OrderedDict

前端 未结 1 927
孤城傲影
孤城傲影 2021-01-04 13:41

I\'ve been using PyMySQL for a while now and created my own wrapper that I\'m used to to shorthand writing queries. Nonetheless I\'ve been creating CSV files with OrderedDi

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 14:03

    You could use cursors.DictCursorMixin and change its dict_type to collections.OrderedDict (the default is dict):

    from collections import OrderedDict
    from pymysql.cursors import DictCursorMixin, Cursor
    
    class OrderedDictCursor(DictCursorMixin, Cursor):
        dict_type = OrderedDict
    

    Then you can use the new cursor class as shown below

    cursor = conn.cursor(OrderedDictCursor)
    

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