2017-09-25 127 views
-2

我retriving从PSQL数据库中的数据,我得到像我下面的值:的Python:从列表中获取值

list = [('value1','value2')] 

我要存储在两个不同的变量VAR1值和VAR2象下面这样:

var1 = 'value1' 
var2 = 'value2' 

我该如何做到这一点?

我试图做到这一点象下面这样:

var1 = list[0][0] 
var2 = list[0][1] 
print var1 (gives me the output value1 and not 'value1') 
print var2 (gives me the output value2 and not 'value2') 
+0

在shell中,所有的字符串都会输出而没有引号,所以一切正常...... – Vladyslav

+0

'print(type(var1))'如果它说的字符串你很好去。 –

+0

你想要在报价周围显示报价? ''print +''“' – cdarke

回答

0

如果需要实际报价添加到您的字符串,你可以使用:

v1, v2 = map(lambda x: "'{}'".format(x), alist[0]) 

请不要使用保留关键字list到命名你的变量。