我试图制作一个程序,它可以逐行读取两个文本文件,并存储早先在Name_Input
中指定的行(在可变行和第2行中) ,然后去掉任何不是来自字符串的数字。Python 2.7 - 将单个字符串转换为整数
for line in Roster_Inputed:
if Name_Input in line:
line = re.sub('[^0-20]', '', line)
if line == "1":
print(Name_Input + " " + "should have " + line + " " + "ally.")
print " "
else:
print(Name_Input + " " + "should have " + line + " " + "allies.")
print " "
for line2 in Roster_Should_Have:
if Name_Input in line2:
line2 = re.sub('[^0-20]', '', line2)
if line2 == "1":
print(Name_Input + " " + "actually has " + line2 + " " + "ally.")
print " "
else:
print(Name_Input + " " + "actually has " + line2 + " " + "allies.")
print " "
的代码是由含有空格后的名称和数量两个文件读取,它就会继续对它们进行比较,以确定它输出到用户:
if line == line2:
print "All good"
elif line != line2:
print "Check " + Name_Input + "'s " + "spies"
print " "
我它需要什么做的是检查“行”的值是否大于“line2”,但我不能这样做,因为他们是包含数字的字符串。有没有办法将其暂时转换为整数?
这取决于你的意思是“更大”。 – zondo
如果字符串中的值大于另一个(实际数字) – Usiek
那么,字符总是数字? – zondo