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

标签 python mysql python-3.x pyqt4 mysql-python

我正在尝试从数据库中获取国家/地区名称并将其粘贴到 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/

相关文章:

python - 获取 GridSearchCV 的标准偏差

python - 如何计算 Pandas 中的逆累积和

mysql - 一个表基于另一表的值

php - 条件不是给变量赋值

python - 使用用户输入调用特定的 python 类

python - 为什么 `tf.data.Dataset.map`只运行一次?

python - 绘制 Sprite 时 openGL/Pyglet 纹理图形消失

mysql - 从 MySQL 导出的 CSV 未出现在指定目录中

python - 将已见集用于有向图与无向图

python - 根据使用 Pandas 的条件动态改变日期