首页 > 解决方案 > 使用 SQLalchemy 将整数加载到 PostgreSQL - 错误 Invalid input syntax for integer

问题描述

我有以下代码将 CSV 文件加载到 PostgreSQL 数据库中。它像这样工作得很好:

def Load_Data(file_name):
    data = genfromtxt(file_name, delimiter=',', skip_header=1, dtype=[('curr', numpy.unicode_, 10),
                                                                      ('code', numpy.unicode_, 10),
                                                                      ('exp', numpy.unicode_, 10),
                                                                      ('cryp', numpy.unicode_, 10)])

    return data.tolist()


Base = declarative_base()


class CurrencyDetails(Base):

    __tablename__ = 'curr_det'
    curr = Column(VARCHAR(10), primary_key=True)
    code = Column(VARCHAR(10))
    exp = Column(VARCHAR(10))
    cryp = Column(VARCHAR(10), nullable=False)

但是 exp 列实际上是 Integer 数据类型,我更愿意使用整数类型创建表列 exp。我试过用

exp = Column(Integer)

但后来我得到了错误

DataError('(psycopg2.DataError) 整数的无效输入语法

源 csv 文件有一些空值'',我认为这是问题所在。我可以在不修改文件的情况下以某种方式加载整数吗?

非常感谢您的帮助!

标签: pythonpostgresqlsqlalchemy

解决方案


推荐阅读