我有这样一个数据
id, Name, Remarks, status
1, Juan, "Hello, World", active
2, Pepito, "Howdy, how are you", inactive
我正在尝试使用python,psycopg2将其加载到postgres
import csv
import psycopg2
try:
next(f)
cursor.copy_from(f, table_name, sep=',', quotechar='"')
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
# os.remove(path)
print("Error: %s" % error)
conn.rollback()
cursor.close()
print(1)
print(f"Data successfully imported to {table_name}.")
cursor.close()
我正在获取错误
Error: 'quotechar' is an invalid keyword argument for this function
到表的目标输出是
id|Name |Remarks |status
1 |Juan |Hello, World |active
2 |Pepito |Howdy, how are you |inactive
1条答案
按热度按时间z5btuh9x1#
如错误所示,方法copy_from的参数quotechar不存在,链接到psycopg2.cursor的文档:https://www.psycopg.org/docs/cursor.html
我的方法是将csv信息提取到python字典或列表中,然后使用psycopg2,这样你就可以在提交到数据库之前用python内置的方法修改字符串。