Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从行中删除项目_Python_Postgresql_Psycopg2 - Fatal编程技术网

Python 从行中删除项目

Python 从行中删除项目,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,有没有一种干净的方法可以从psycopg2 DictRow对象中删除项及其值和索引 我正在使用psycopg2与两个不同的postgresql数据库进行交互。我想将一条记录作为一行从一个数据库复制到另一个数据库,并且我想在插入记录之前从记录中删除几个字段 例如,如果我有一个名为row的行: 使用row.popINDEX会更改列表的索引,因此存储在该行中的键。\u index属性不再正确 >>> row.pop(0) 12 >>> print row ['20

有没有一种干净的方法可以从psycopg2 DictRow对象中删除项及其值和索引

我正在使用psycopg2与两个不同的postgresql数据库进行交互。我想将一条记录作为一行从一个数据库复制到另一个数据库,并且我想在插入记录之前从记录中删除几个字段

例如,如果我有一个名为row的行:

使用row.popINDEX会更改列表的索引,因此存储在该行中的键。\u index属性不再正确

>>> row.pop(0)
12

>>> print row
['2015-12-31', 'kefir sriracha put a bird on it']

>>> print row.keys()
['id', 'created_at', 'output_example']

>>> print row['id']
'2015-12-31' # GAAAAA!!!
无法使用删除行['id']:TypeError。我认为这是因为底层对象是一个列表,需要一个整数索引

我目前的解决办法是这样做,这似乎不雅观:

fields = [f for f in row.keys() if f != 'id']
values = [v for k, v in row.iteritems() if k != 'id']
有没有关于更直接地从行中删除项目的建议

使用

输出:

{'two': 2, 'one': 1}
{'two': 2}
使用

输出:

{'two': 2, 'one': 1}
{'two': 2}

我一直在使用的解决方案是获取DictRow并将其转换为dict


它确实改变了数据类型,所以这不是我提出的问题的一个很好的答案。在这种情况下,重构依赖于同一游标及其行为的其他代码要容易得多。

我一直在使用的解决方案是获取DictRow并将其转换为dict


它确实改变了数据类型,所以这不是我提出的问题的一个很好的答案。在这种特定情况下,它比重构依赖于同一游标及其行为的其他代码更容易。

示例:在psycopg2==2.8.4下,python 3.7

你可能已经解决了这个问题,但我正在发布,以防其他人需要这个

# We want to exclude column 'a' in row.
row: DictRow
# row = {'a': value1, 'b': value2}
# row._index = OrderedDict({'a': 0, 'b': 1})

remove_columns = ['a']
remove_columns_index_set = set()
# Find the index of the column to exclude. a -> 0.
for col in remove_columns:
  remove_columns_index_set.add(row._index.get(col))

# Override the list with new values. (I find this line in DictRow's __init__)
row[:] = [col for idx, col in enumerate(row) if idx not in remove_columns_index_set]

# Delete the index of removed column.
# Because _index in row is shared among all rows, we only need to update it once.
for remove_col in remove_columns:
  del row._index[remove_col]

# Update the DictRow index to new value.
for idx, column in enumerate(row._index):
  row._index[column] = idx
我也在寻找同样的问题


有冲动在我的代码库中包含并重写DictRow:

示例:在psycopg2==2.8.4、python 3.7下

你可能已经解决了这个问题,但我正在发布,以防其他人需要这个

# We want to exclude column 'a' in row.
row: DictRow
# row = {'a': value1, 'b': value2}
# row._index = OrderedDict({'a': 0, 'b': 1})

remove_columns = ['a']
remove_columns_index_set = set()
# Find the index of the column to exclude. a -> 0.
for col in remove_columns:
  remove_columns_index_set.add(row._index.get(col))

# Override the list with new values. (I find this line in DictRow's __init__)
row[:] = [col for idx, col in enumerate(row) if idx not in remove_columns_index_set]

# Delete the index of removed column.
# Because _index in row is shared among all rows, we only need to update it once.
for remove_col in remove_columns:
  del row._index[remove_col]

# Update the DictRow index to new value.
for idx, column in enumerate(row._index):
  row._index[column] = idx
我也在寻找同样的问题


有冲动在我的代码库中包含并重写DictRow:

如何使用row.keys?行不是如您的示例中所示的列表吗?它看起来是一种完全错误的方法。你能解释一下为什么你需要使用Python并返回Postgresql吗?@kmario23:这是一个基于列表的对象,但它是一个DictRow:@ClodoaldoNeto:我正在脚本中做一些我需要Python的其他事情。我听到你说的了-在postgresql中完成这一部分会更干净。如何使用row.keys?行不是如您的示例中所示的列表吗?它看起来是一种完全错误的方法。你能解释一下为什么你需要使用Python并返回Postgresql吗?@kmario23:这是一个基于列表的对象,但它是一个DictRow:@ClodoaldoNeto:我正在脚本中做一些我需要Python的其他事情。我听到你说的了-在postgresql中做这部分会更干净。我必须重构一堆依赖DictCursor的代码,所以我不愿意这样做。我必须重构一堆依赖DictCursor的代码,所以我不愿意这样做。是的,我从社区复制/学习/窃取了很多代码:是的,我从社区复制/学习/窃取了很多代码:
# We want to exclude column 'a' in row.
row: DictRow
# row = {'a': value1, 'b': value2}
# row._index = OrderedDict({'a': 0, 'b': 1})

remove_columns = ['a']
remove_columns_index_set = set()
# Find the index of the column to exclude. a -> 0.
for col in remove_columns:
  remove_columns_index_set.add(row._index.get(col))

# Override the list with new values. (I find this line in DictRow's __init__)
row[:] = [col for idx, col in enumerate(row) if idx not in remove_columns_index_set]

# Delete the index of removed column.
# Because _index in row is shared among all rows, we only need to update it once.
for remove_col in remove_columns:
  del row._index[remove_col]

# Update the DictRow index to new value.
for idx, column in enumerate(row._index):
  row._index[column] = idx