我正在尝试使用python生成一个在redshift中生成卸载命令的脚本。我不是一位专家的Python程序员。我需要在哪里可以为卸载列表生成所有列。如果该列是特定名称,则需要使用函数进行替换。我面临的挑战是将“,”附加到字典中的最后一项。有没有办法避免最后的逗号?任何帮助,将不胜感激。从字典/列表中删除最后一个逗号
import psycopg2 from psycopg2.extras
import RealDictCursor
try:
conn = psycopg2.connect("dbname='test' port='5439' user='scott' host='something.redshift.amazonaws.com' password='tiger'");
except:
print "Unable to connect to the database"
conn.cursor_factory = RealDictCursor
cur = conn.cursor()
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
try:
cur.execute("SELECT column from pg_table_def where schema_name='myschema' and table_name ='tab1' ");
except:
print "Unable to execute select statement from the database!"
result = cur.fetchall()
print "unload mychema.tab1 (select "
for row in result:
for key,value in row.items():
print "%s,"%(value)
print ") AWS Credentials here on..."
conn.close()
为什么我收到一个错误NameError:名字“加入”没有定义。 的行结果: 打印加入( “”[值值row.values()]) 打印 “)到S3://” conn.close() – paddu 2014-10-20 21:01:47
对不起,是我不好。尝试更正的版本。 – 2014-10-20 21:26:21