2 Replies - 510 Views - Last Post: 13 December 2012 - 07:34 AM Rate Topic: -----

#1 sitdownson  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

Reading from a text file into list then writing back to list w/ CGI

Posted 13 December 2012 - 07:17 AM

I have a cgi script that i want the user to type in a keyword in. There is a file keywords.txt that contains keywords. The program will open that file and make it into a list. Then, it should check to user if the keyword the user just entered is in that list. If it is, it will take it to another cgi script, if it is not, it will write it to the file and take it to a different cgi script.

I am having trouble figuring out what variable to use in my IF statement to check if the user-inputed keyword is in the list. Here is what I have so far, like I said my main problem if figuring out how to check if it is in the list and then adding it to the text file.

#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()
keyword = form.getvalue('keyword')

print 'Content-type: text/html\r\n\r'
print '<html>'
print '<h1>Please enter a keyword of your choice</h1>'
print '<form action="results.cgi" method="post">'
print 'Keyword: <input type="text" name="keyword">  <br />'
print '<input type="submit" value="Submit" />'
print '</form>'
print '</html>'

#keylist = []
#f = open('keywords.txt', 'rw')
#for each in f.readlines():
    #keylist.append(each)

#if keyword in keylist:
    #print 'Location: %s' % #Second CGI Script

#else:
    #f.write(keyword)
    #f.close()
    #print 'Location: %s' % #Third CGI Script


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a text file into list then writing back to list w/ CGI

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,686
  • Joined: 13-March 10

Re: Reading from a text file into list then writing back to list w/ CGI

Posted 13 December 2012 - 07:29 AM

Make your functions be responsible for a well-defined tasks. For example, here is a function that checks whether a value is in the list, and it returns a boolean value. You can use it then, in an if-statement to add word to a file dependent on the outcome.

#method that checks if the value is in the list
#lst - list to be searched
#value - value that the user has entered
#output - True if the value is in the list - otherwise False

def checkList(lst, value):
    if len(lst) == 0:
        return False
    
    if lst[0] == value:
        return True
    
    return checkList(lst[1:], value)


Was This Post Helpful? 0
  • +
  • -

#3 sitdownson  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 27-September 11

Re: Reading from a text file into list then writing back to list w/ CGI

Posted 13 December 2012 - 07:34 AM

My main problem is that I don't know what the variable for the if statement should be. I don't see in my code what variable the user-inputed word would be
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1