我正在嘗試使用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