这个问题在这里已有答案:
我想比较两个变量:
node_latest_version
- 来自刮installed_version
- 通过执行node -v
并将值存储在变量上。
在执行时显示相同的值:
print(node_latest_version)
# prints v11.13.0
print(installed_version)
# prints v11.13.0
但是当我试图比较两者时
if node_latest_version == installed_version:
print('success')
# No response
我试过了:
if node_latest_version == 'v11.13.0':
print('OK')
# this prints OK
if installed_version == 'v11.13.0':
print('OK')
# NO RESPONSE
也
if type(node_latest_version) == str:
print('OK')
# this prints OK
if type(installed_version) == str:
print('OK')
# this prints OK
这是我的代码:
node_latest_version = results[1].attrs['data-version']
installed_version = subprocess.Popen("node -v",
shell=True,
stdout=subprocess.PIPE,
universal_newlines=True).communicate()[0]
print(node_latest_version)
print(installed_version)
if node_latest_version == installed_version:
print('OK')
我希望比较这两个值
在比较之前对两个字符串使用.strip()方法。 installed.strip()== latest.strip()
试试这个并比较:
node_latest_version = ''.join(x for x in node_latest_version if x.isdigit() or x == '.')
installed_version = ''.join(x for x in installed_version if x.isdigit() or x == '.')
if node_latest_version == installed_version:
print('OK')