2017-01-22 66 views
0

我要搜索用户的输入值,但只有在该行的第一部分搜索一行文件行线的第一部分(蟒蛇)

def optionA(): 
    fixnum = input("What is the fixture number of the game?") 
    with open("firesideFixtures.txt") as f:#open the file as "f" 
      for line in f:#search each line 
       if str(fixnum) in line:#search each line for the user input 
         print(line)#print the line 

optionA() 

的问题是它的打印每与其中的夹具号码一致。我需要它只打印夹具号码是第一个字符的那个。
这里的文件:

1,02/09/15,18:00,RNGesus,Ingsoc,Y,Ingsoc 
    2,03/09/15,18:00,M'lady,Napoleon Wilson,Y,Napolean Wilson 

回答

2

使用startswith,则:

if line.startswith(str(fixnum)): 
+0

的Python 2将与您认为Fixnum对象并不总是一个字符串;) –