Nit*_*zle 18 python oracle cx-oracle
我一直在使用以下函数来制作一个"更具可读性"(可能)的格式,用于从Oracle获取数据.这是功能:
def rows_to_dict_list(cursor):
"""
Create a list, each item contains a dictionary outlined like so:
{ "col1_name" : col1_data }
Each item in the list is technically one row of data with named columns,
represented as a dictionary object
For example:
list = [
{"col1":1234567, "col2":1234, "col3":123456, "col4":BLAH},
{"col1":7654321, "col2":1234, "col3":123456, "col4":BLAH}
]
"""
# Get all the column names of the query.
# Each column name corresponds to the row index
#
# cursor.description returns a list of tuples,
# with the 0th item in the tuple being the actual column name.
# everything after i[0] is just misc Oracle info (e.g. datatype, size)
columns = [i[0] for i in cursor.description]
new_list = []
for row in cursor:
row_dict = dict()
for col in columns:
# Create a new dictionary with field names as the key,
# row data as the value.
#
# Then add this dictionary to the new_list
row_dict[col] = row[columns.index(col)]
new_list.append(row_dict)
return new_list
Run Code Online (Sandbox Code Playgroud)
然后我会使用这样的函数:
sql = "Some kind of SQL statement"
curs.execute(sql)
data = rows_to_dict_list(curs)
#
for row in data:
item1 = row["col1"]
item2 = row["col2"]
# Do stuff with item1, item2, etc...
# You don't necessarily have to assign them to variables,
# but you get the idea.
Run Code Online (Sandbox Code Playgroud)
虽然这似乎在不同程度的压力下表现相当好,但我想知道是否有更高效或"pythonic"的方式来做到这一点.
sen*_*rle 26
还有其他一些改进,但这真的让我跳了起来:
for col in columns:
# Create a new dictionary with field names as the key,
# row data as the value.
#
# Then add this dictionary to the new_list
row_dict[col] = row[columns.index(col)]
Run Code Online (Sandbox Code Playgroud)
除了效率低之外,index在这样的情况下使用是容易出错的,至少在列表中可能出现两次相同项目的情况下.enumerate改为使用:
for i, col in enumerate(columns):
# Create a new dictionary with field names as the key,
# row data as the value.
#
# Then add this dictionary to the new_list
row_dict[col] = row[i]
Run Code Online (Sandbox Code Playgroud)
但那真的是小土豆.这是这个函数的一个更紧凑的版本:
def rows_to_dict_list(cursor):
columns = [i[0] for i in cursor.description]
return [dict(zip(columns, row)) for row in cursor]
Run Code Online (Sandbox Code Playgroud)
如果有效,请告诉我.
Jos*_*rts 10
为了避免在预先列出列表中的所有内容的内存使用的干净方法,您可以将光标包装在生成器函数中:
def rows_as_dicts(cursor):
""" returns cx_Oracle rows as dicts """
colnames = [i[0] for i in cursor.description]
for row in cursor:
yield dict(zip(colnames, row))
Run Code Online (Sandbox Code Playgroud)
然后使用如下 - 迭代时将游标中的行转换为dicts:
for row in rows_as_dicts(cursor):
item1 = row["col1"]
item2 = row["col2"]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21744 次 |
| 最近记录: |