导入csv上的psycopg2问题

时间:2018-04-26 03:03:48

标签: python file 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()
  

我正在尝试这段代码跟踪

     

在import_from_csv中的文件" migrate.py",第29行       dest_cur.copy_from(f,table_name,sep =&#34 ;;",null =' \ N',columns = common_cols_tup)   psycopg2.extensions.QueryCanceledError:来自stdin的COPY失败:.read()中的错误调用:exceptions.ValueError混合迭代和读取方法会丢失数据   背景:COPY res_partner,第1行

2 个答案:

答案 0 :(得分:0)

关键是内部错误:

C

根据this SO response,错误来自同一文件句柄上同时使用exceptions.ValueError Mixing iteration and read methods would lose data next。如果您使用readline跳过标题行,我认为应该没问题。

答案 1 :(得分:0)

我像

一样解决了它
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()