我正在尝试检测软件版本是否是最新的,我在 Python 3.3 中使用以下代码来执行此操作:
if str(version) in str(buildno):
print("Your software version is up to date. \n")
else:
print("Your software is out of date. Updating your software. \n")
但是,即使软件是最新的,它也会不断更新软件。我也试过代码变体:
if str(version) in str(buildno) == True:
print("Your software version is up to date. \n")
else:
print("Your software is out of date. Updating your software. \n")
if os == "Windows":
subprocess.call("windowsUpgrade.sh", shell=True)
这也行不通。我使用的方法是否可行,或者我应该采用另一种方法来解决这个问题?
>>> print(version)
4.3.0-18107
>>> print(buildno)
('4.3.0-18107', 1)
感谢您提供的任何答案。
最佳答案
好吧,这里使用的数据类型似乎有些困惑
元组到字符串:
str(('4.3.0-18107', 1)) = "('4.3.0-18107', 1)"
不在字符串中的元组:
if "('4.3.0-18107', 1)" in '4.3.0-18107' # False
元组中的字符串
if '4.3.0-18107' in "('4.3.0-18107', 1)" # True
String in (first index Tuple = String)
if '4.3.0-18107' in ('4.3.0-18107', 1)[0] # True
如果顺序无关紧要,您需要在转换为字符串之前对元组 str(('4.3.0-18107', 1)[0]) 建立索引。您在上面的代码中所做的是将元组转换为字符串而不是版本。因此,帕维尔·阿诺索夫 (Pavel Anossov) 认为交换应该在这里起作用是正确的 - 至少它对我有用。
所以这最终奏效了(遗漏了一个空格):
buildno=buildno[0]
version=str(version.strip())
buildno=str(buildno.strip())
if version == buildno
或更短:
if str(version).strip() == str(buildno[0]).strip():
if str(version).strip() in str(buildno[0]).strip():
关于python - 检测一个字符串是否在另一个字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135962/