gpt4 book ai didi

python - 如何从多个字符串中删除某些字符?

转载 作者:行者123 更新时间:2023-11-29 10:59:21 33 4
gpt4 key购买 nike

我正在尝试从数据库中获取国家/地区名称并将其粘贴到 QComboBox。一切工作正常,除了来自数据库表的元组,如 ('Afghanistan',) 、 ('Angola',) ... 问题是如何从元组中删除无用的字符。代码是:

countries = []
try:
conn = MySQLdb.connect(host='localhost', user='root', passwd='*****', db='world')
cursor = conn.cursor()
try:
cursor.execute("SELECT Name from country")
while True:
rows = cursor.fetchone()
if rows == None:
break
else:
countries.append(rows)
except:
print('Error CURSOR')
cursor.close()
conn.close()
except:
pass
for i in countries:
self.country_cbox.addItem(str(i)) # Converting to string
print(countries)
self.add_worker.show()
self.add_worker.exec_()

enter image description here

最佳答案

您可以使用列名或元组索引来删除值:

countries.append(rows.Name)  # column name
# or use
rows[0] # column index

关于python - 如何从多个字符串中删除某些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42514598/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com