返回字典时的光标并打印键在哪里

Cursor when returning dictionary and print where are the keys

我正在尝试理解游标返回的数据结构

我有以下代码:

con = psycopg2.connect("dbname='testdb2' user='kevin'") 

cursor = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute("SELECT * FROM Cars")

rows = cursor.fetchall()

for row in rows:
    print row["id"], row["name"], row["price"]

输出:

1 奥迪 52642

2 奔驰 57127

3 斯柯达 9000

等....

如果我说

for row in rows:
    print rows

它输出

[[1, 'Audi', 52642], [2, 'Mercedes', 57127], [3, 'Skoda', 9000], [4, 'Volvo', 29000], [5, 'Bentley', 350000], [6, 'Citroen', 21000], [7, 'Hummer', 41400], [8, 'Volkswagen', 21600]]

钥匙在哪里?我期待这样的输出

[['Id': '1' , 'name':'Audi', 'price:'52642'], ['Id': '2' , 'name':'Mercedes', 'price:'57127'] ....etc

我不确定是否因为我缺乏理解 python 我确实期望得到那个输出。

每个 rown 都是一个 DictRow 继承自 list:

https://github.com/psycopg/psycopg2/blob/master/lib/extras.py

class DictRow(list):
    """A row object that allow by-column-name access to data."""

    __slots__ = ('_index',)

    def __init__(self, cursor):
        self._index = cursor.index
        self[:] = [None] * len(cursor.description)

    def __getitem__(self, x):
        if not isinstance(x, (int, slice)):
            x = self._index[x]
        return list.__getitem__(self, x)

    def __setitem__(self, x, v):
        if not isinstance(x, (int, slice)):
            x = self._index[x]
        list.__setitem__(self, x, v)

    def items(self):
        return list(self.iteritems())

    def keys(self):
        return self._index.keys()

    def values(self):
        return tuple(self[:])

    def has_key(self, x):
        return x in self._index

    def get(self, x, default=None):
        try:
            return self[x]
        except:
            return default

    def iteritems(self):
        for n, v in self._index.iteritems():
            yield n, list.__getitem__(self, v)

    def iterkeys(self):
        return self._index.iterkeys()

    def itervalues(self):
        return list.__iter__(self)

    def copy(self):
        return dict(self.iteritems())

    def __contains__(self, x):
        return x in self._index

    def __getstate__(self):
        return self[:], self._index.copy()

    def __setstate__(self, data):
        self[:] = data[0]
        self._index = data[1]

    if _sys.version_info[0] > 2:
        items = iteritems; del iteritems
        keys = iterkeys; del iterkeys
        values = itervalues; del itervalues
        del has_key