我有这样一个街区:
example of list:
pommes : 54
bananes : 18 : 99
orange
你知道吗Ìt检验左字符串和右字符串是否用:分隔,右字符串的类型是int
我想把多余的“:99”当作一个错误来抓。另外,ifmy:不存在。如果有任何方法可以将其添加到gauche2[2]
for i in message:
parts = i.split(":")
gauche = parts[0].strip()
droite = parts[1]
try:
droite = int(droite)
if not gauche.isalpha():
print("La ligne '", i, "' n'est pas correctement formaté.")
sys.exit()
except ValueError:
print("La ligne '", i, "' n'est pas correctement formaté.")
sys.exit()
您可以使用split()和len:
举个例子:
相关问题 更多 >
编程相关推荐