#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11

    Simple function question


    Hi,

    Could somebody please explain what is missing here, that it does not work or returns only one item in the list were I know that the second exists and there are three occurances.

    Code:
    list = ['81609','406AE']
    
    input = file('D:\\Temp Files\\log.txt')
    
    def test(item):		
        for line in input:
        	if line.find(item)!=-1:
        		print line[:-1]
    
    for item in list:
    	test(item)
    Thanks
    Random
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Here is what is happening:

    1) you open the file and assign it to 'input'

    2) you call test() on the first element of the array, '81609'

    3) test() goes through the file a line at a time, and if it finds a line with the value in it prints it out.

    So far, so good...

    4) test() is called on the second value in the list, '406AE'

    5) test tries to iterate through the file, but since it has already reached the end it returns immediately.

    Oops... no match for the second or later calls to test.

    You can only iterate through a file once. If you try to iterate through it a second time it will fail, since it is already at the end of the file.

    Some possible solutions:

    a) close and reopen the file after each call to test()
    or
    b) iterate once through the file, and on each line iterate though the list and do the comparison.

    My preference is for b. Re-write the test function to take as parameters a file object and a list of strings to search for. Something like:

    Code:
    def test(fileObj, searchStrings):
      for line in fileObj:
         for element in searchStrings:
             if element in line:
                  print line.rstrip()
    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11
    Thanks DevCoach.

    Now did I do this right? I used the file news.txt that came with python, but the result only matched the first element of the list macros.

    Code:
    input = file('C:\Python24\Samples\NEWS.txt')
    
    def test(input, searchStrings):
      for line in input:
         for element in searchStrings:
             if element in line:
                  print line.rstrip()
                  
    test(input,('macros','allocates'))
    The result was

    - Added C macros Py_CLEAR and Py_VISIT to ease the implementation of
    - A large pile of datetime field-extraction macros is now documented,
    - mention the labor saving Py_VISIT and Py_CLEAR macros.
    intermediate sequences. Also, extend() now pre-allocates the
    containing type-check macros and constructors. See new docs in the
    functionality of the Py_XINCREF() and Py_XDECREF macros. Useful for
    - Added three new macros: Py_RETURN_NONE, Py_RETURN_TRUE, and Py_RETURN_FALSE.
    and PyAPI_DATA macros, while Python extension module init functions
    will be declared with PyMODINIT_FUNC. DL_EXPORT/DL_IMPORT macros
    - PyObject_Del, PyObject_GC_Del are now functions instead of macros.


    Thanks
    Random
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11
    oops sorry missed it. Yes it found the other element from the list.

    Thanks again
    Random

IMN logo majestic logo threadwatch logo seochat tools logo