2014-01-08 123 views
-1

以下织物代码返回蟒字符串比较不工作

[email protected]:~# cat fabfile.py 
import fabric 
def lsb_info(): 
    ReleaseNum=fabric.api.local('lsb_release --release|awk \'{print $2}\'', capture=True) 
    print ReleaseNum == 12.04 
    print ReleaseNum 


[email protected]:~# fab lsb_info 
[localhost] local: lsb_release --release|awk '{print $2}' 
False 
12.04 

Eventhough的ReleaseNum填充为12.04它不是在字符串比较等于12.04

+0

你应该使用周围的引号:'“12.04”',否则会被视为浮动对象。 –

+0

主要的问题放在一边,那感觉真是奇怪使用'从这样的Python脚本awk' ... – NPE

+2

你应该学习Python第.. –

回答

1

由于12.04是float而不是字符串,加引号呢,像这样:

print ReleaseNum == '12.04' 

由于ReleaseNum是一个字符串,当你与进行比较3210返回假的,因为他们是不同类型的,所以当你通过把引号转换12.04为一个字符串,它的工作原理...