Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我创建的表单上有一个组合框。此组合框同时显示 ID 和名称。我正在将组合框的内容写入数据库。到目前为止,当我使用这段代码时它可以正确写入
self.ui.residentComboBox.currentText()
现在我想要做的是只从组合框中提取 ID,而不是将 ID 和名称放在一起。任何人都可以帮忙吗?
如果self.ui.residentComboBox.currentText()返回一个字符串,并且 ID 只是第一个单词,你可以这样做:
self.ui.residentComboBox.currentText().split()[0]
它将字符串拆分为单词列表(由空格分隔),然后[0]给出该列表中的第一项。
[0]
例如:
t = "3426523 askew chan" print t.split() #['3426523','askew','chan'] print t.split()[0] #'3426523'