从sqlite结果转换值

时间:2019-03-15 09:54:13

标签: python pyqt4

Issue Image and expected output

我在从列表转换为python中的单独字符串时遇到麻烦。下面是用于获取字符串和异常的代码:

代码:

mycursor.execute("SELECT fid,contents,input_type  FROM frame_report1")

myresult = mycursor.fetchall()
print myresult
# myresult --->[(449, u'text1', u'checkbox'), (454, u'text2', u'textbox'), (455, u'text3', u'textbox')]
print type(myresult)#list type
for x,x1,x2 in myresult:
    g = int(''.join(map(str, x))) #int type
    l = ''.join( x1) #string type
    k = ''.join( x2) #string type

例外:

g = ''.join(map(str, x))
TypeError: argument 2 to map() must support iteration

我想将值从myresult转换为变量glk

编辑: @Matt B:感谢您的答复,我使用了您提到的代码,但没有帮助,我们需要在单独的String数组中获取行内容449、454 455,目前正在合并。请提供指导,谢谢,请检查所附的图像,其中显示了问题图像和预期图像。

代码:

myresult = [(449, u'text1', u'checkbox'), (454, u'text2', u'textbox'), (455, u'text3', u'textbox')]
row = 0
for x, x1, x2 in myresult:
    g = int(''.join(map(str, x)))  # int type

    l = ''.join(x1)  # string type
    k = ''.join(x2)  # string type
    rcount_general = self.tableWidget.rowCount()
    self.tableWidget.insertRow(rcount_general)
    r = 0
    for i in range(rcount_general + 1):
        self.tableWidget.setItem(row, 0, QtGui.QTableWidgetItem(g))
        self.tableWidget.setItem(row, 1, QtGui.QTableWidgetItem(l))
        self.tableWidget.setItem(row, 2, QtGui.QTableWidgetItem(k))        
        r = r + 1
    row = row + 1

2 个答案:

答案 0 :(得分:1)

不必使用join,您只需要在枚举的帮助下进行迭代:

for row, result in enumerate(myresult):
    self.tableWidget.insertRow(self.tableWidget.rowCount())
    for col, value in enumerate(result):
        self.tableWidget.setItem(row, col, QtWidgets.QTableWidgetItem(str(value)))

答案 1 :(得分:0)

这是您要寻找的吗?

myresult = [(449, u'text1', u'checkbox'), (454, u'text2', u'textbox'), (455, u'text3', u'textbox')]

g = ''.join([str(x[0]) for x in myresult])
l = ''.join([str(x[1]) for x in myresult])
k = ''.join([str(x[2]) for x in myresult])

print(g)
print(l)
print(k)

输出:

449454455

text1text2text3

checkboxtextboxtextbox