0

姓、名、年齢、性別などの人物の詳細を含む html ファイルがあります。
これらのhtmlファイルからデータを取得してxyzファイルに保存するpythonファイルがあります。
今、私はテキストボックスに何かを入力するとファイルを検索する検索ボタンが欲しい

私のindex.htmlファイル:

<html>
<head>
<title>INFORMATION</title>
</head>
  <body>
    <form action = "/cgi-bin/test.py" method = "post">
    FirstName:
    <input type = "text" name = "firstname" /><br>
    LastName:
    <input type = "text" name = "lastname" /><br>
    <input type = "submit" name = "submit "value = "SUBMIT">
    <input type = "reset" name = "reset" value = "RESET">
    <input type = "button" name = "search" value = "SEARCH">
    </form>
 </body>
</html>

私のtest.pyファイル:

#!/usr/bin/python
import cgi

def get_data():
    '''
This function writes the form data into the file 

    '''
    form = cgi.FieldStorage()

    Fname = form.getvalue('firstname')
    Lname = form.getvalue('lastname')
    Age = form.getvalue('age')
    Gender = form.getvalue('gender')
    f = open("/tmp/abc.txt","a")
    f.write(Fname + ' ' + Lname + ' ' + Age + ' ' + Gender + '\n')
    f.close()

    print "Content- type : text/html\n"
    print "Hello ", Fname, Lname, Age, Gender
    print "You have successsfully written your data into a file"

get_data()

Pythonコードでこの検索を行うには、何を書く必要がありますか? 何を書けばいいの?

4

1 に答える 1

0

作成したばかりのファイルで単語を検索する必要がある場合:

with open('file.txt', 'r') as f:
for line in f:
    if 'wordToLookFor' in line:
        print "found it in : %s" % line
于 2013-10-25T13:22:11.607 に答える