使用 SQLalchemy for PostgreSQL 去除双引号

Get rid of double quotation marks with SQLalchemy for PostgreSQL

我正在尝试将 200 个 SAS XPT 文件导入我的 PostgreSQL 数据库:

engine = create_engine('postgresql://user:pwd@server:5432/dbName')
for file in listdir(dataPath):
    name, ext = file.split('.', 1)
    with open(join(dataPath, file), 'rb') as f:
        xport.to_dataframe(f).to_sql(name, engine, schema='schemaName', if_exists='replace', index=False)
    print("Successfully wrote ", file, " to database.")

但是,生成的SQL所有标识符都有双引号,例如:CREATE TABLE "Y2009"."ACQ_F" ("SEQN" FLOAT(53), "ACD010A" FLOAT(53));。问题是,如果列 /table/schema 是用引号创建的,每次我需要查询它们时,我都必须包括引号,同时 use the exact capitalization.

我想去掉引号,但我不能自己写自定义 SQLs,因为这些文件的结构各不相同。

PostgreSQL 需要大写 table / 列名被引用 (reference)。这就是为什么引用由 SQLalchemy 构造的 SQL 中的标识符。为避免这种情况,将数据框的列名转换为全部小写:

with open(join(dataPath, file), 'rb') as f:
     data = xport.to_dataframe(f)
     data.columns = map(str.lower, data.columns)
     data.to_sql(name.lower(), engine, schema='y2007')