逐行搜索文件的第一部分(python)

时间:2017-01-22 15:16:53

标签: python

我必须搜索用户输入值,但仅限于行的第一部分

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

1 个答案:

答案 0 :(得分:2)

然后使用startswith

if line.startswith(str(fixnum)):