进口CSV上的Psycopg2问题


def import_from_csv(common_cols_tup, table_name):
"""
:param common_cols_tup: tuple of all columns
:param table_name: database table name
:return:
"""
with open('/tmp/%s.csv'%table_name, 'r') as f:
    # Notice that we don't need the `csv` module.
    next(f)  # Skip the header row.
    dest_cur.copy_from(f, table_name, sep=";",null='\N', columns=common_cols_tup)
dest_cur.commit()

我正在尝试尝试此代码

以下痕迹

文件" migrate.py",第29行,incorm_from_csv dest_cur.copy_from(f,table_name,sep =";",null =' n',列= common_cols_tup( psycopg2.extensions.queryCanceLedError:stdin fafe fafe fafe fafe:read((呼叫中的错误 上下文:复制res_partner,第1行

关键是内部错误:

exceptions.ValueError Mixing iteration and read methods would lose data 

根据此响应,错误来自在同一文件句柄上同时使用nextreadline。如果您使用readline跳过标题线,您认为应该可以。

我像

一样解决了它
def import_from_csv(common_cols_tup, table_name):
    """
    :param common_cols_tup: tuple of all columns
    :param table_name: database table name
    :return:
    """
    with open('/tmp/%s.csv'%table_name, 'r') as f:
        # Notice that we don't need the `csv` module.
        next(f)  # Skip the header row.
        content = StringIO('n'.join(line for line in f))
        dest_cur.copy_from(content, table_name, sep=";",null='\N', columns=common_cols_tup)
    dest_cur.commit()

最新更新