0
while True:
    try:
        OpenFile=raw_input(str("Please enter a file name: ")) 
        infile=open(OpenFile,"r")
        contents=infile.readlines()
        infile.close()

        user_input = raw_input(str("Enter A=<animal> for animal search or B=<where lives?> for place of living search: \n")) 
        if user_input.startswith("A="):
            def find_animal(user_input,column):
                return next(("\t".join(line) for line in contents
                             if line[column-1]==user_input),None)
            find_animal(user_input[1:]) 
            print str((find_animal(user_input[1:], "WHO?"))) #"Who?" is the name of the first column.

        else:
            print "Unknown option!"


    except IOError:
        print "File with this name does not exist!"

1.動物の名前を入力します。

2.プログラムは、最初の列にこの特定の名前を持つ行を検索します。

3.プログラムは、最初の列にこの名前を持つ行を出力します。

私の機能はここでは正しく動作していないようです。間違いを見つけるのを手伝ってもらえますか?ありがとうございました!

編集

      def ask_for_filename():
         filename=str(raw_input("Please enter file name: "))
         return filename

      def read_data(filename): 
         contents=open(filename,"r")
         data=contents.read()
         return data

      def  column_matches(line, substring, which_column):  
         for line in data:
             if column_matches(line, substring, 0):
                print line
4

1 に答える 1