我試圖製作一個程序,它可以逐行讀取兩個文本文件,並存儲早先在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