繁体   English   中英

如何将元组列表转换为 Python 中的普通列表或从元组中删除部分文本?

[英]How to convert a tuple list to a normal one in Python or remove parts of text from tuple?

sql = '''select datname from pg_database; '''

cursor.execute(sql)
dbs=[]
for dbase in cursor.fetchall():
    dbs.append(dbase)

conn.commit()
conn.close()
print(dbs)
clicked = StringVar()
clicked.set(dbs[0])
db_choice = OptionMenu(row1,clicked,*dbs)

        conn = psycopg2.connect(
        host="localhost",
        database=clicked.get(),
        user="postgres",
        password="Testuser12")

    print('Connecting to the PostgreSQL database...')

    cursor = conn.cursor()

    cursor.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'""")
    db_tables = []
    for table in cursor.fetchall():
        print(table)
        db_tables.append(table)

每当我尝试执行我的代码时,我都会收到此错误:

在“localhost”(::1)连接到服务器,端口 5432 失败:致命:数据库“('postgres',)”不存在

我无法删除 (' ',) 部分,因为它们在一个元组中。 我也尝试将元组转换为普通列表,但它似乎不起作用,我收到此错误:

TypeError:列表索引必须是整数或切片,而不是元组

我应该怎么办?

只需访问元组的第一个元素。 如果dbase=('postgres',) ,那么x=dbase[0]将是'postgres'

x=*dbase也可以,但如果您不熟悉,则应避免使用星号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM