如何从多个字符串中删除一些字符?

时间:2017-02-28 16:59:41

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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