我已经在python中编写了一个web.py服务来访问PostGres并获取特定数据库中的表名。从Python中的元组中提取字符串psycopg2
CODE:
def GET(self,r):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
tables = []
datasetID = web.input().dataSetID
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
tablesWithDetails = cursor.fetchall()
print tablesWithDetails
for x in tablesWithDetails:
x.replace("(", "")
x.replace(")","")
tables.append(x)
print tables;
这将打印表如下,
[('acam_datasegregationdetails',), ('acam_datasegregationheader',), ('idn_accessinformation',), ('idn_b2cuseraccountmapping',), ('idn_b2cuserdevicemapping',), ('idn_b2cusers',), ('idn_roles',), ('idn_useraccountmapping')]
所需输出:
['acam_datasegregationdetails', 'acam_datasegregationheader', idn_accessinformation', 'idn_b2cuseraccountmapping', 'idn_b2cuserdevicemapping', 'idn_b2cusers', 'idn_roles', 'idn_useraccountmapping']
试试这个'tables.append(X [0])'如果这不起作用,你可以键入'type(x)' – The6thSense
感谢它的工作 – Sajeetharan
你知道吗它工作? – The6thSense