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

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Programming help in python 2.6


    I need help with a simple python 2.6 programming task.

    I wish to write a code which needs to search a certain text pattern on multiple text files which are parameters in command line, however the number of input files is not constant and can be any number of files.

    I do I read in the code a list of files from command line without knowing the size of the list?

    Also another parameter in command line is a pattern which needs to be skipped during the search. How can skipping a pattern be performed in python?

    Thank you for your help
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Code:
    '''
        $ # invoke doctest on /tmp/p.py using bash.
        $ ( cd /tmp && python -m doctest p.py )
    '''
    import re
    import sys
    import argparse # if you can understand this, it's good to use.
    
    def accept(fyeah,fnay,strings):
        return [s for s in strings if fyeah(s) and not fnay(s)]
    
    def main(keep_pattern,discard_pattern,strings):
        '''
            >>> main(*('a','b','a b ab xa xab xb yzs'.split()))
            ['a', 'xa']
        '''
        y = re.compile(keep_pattern)
        n = re.compile(discard_pattern)
        return accept(y.search,n.search,strings)
    
    if '__main__' == __name__:
        argv = sys.argv
        try:
            retain,discard = argv[1:3]
            file_names = argv[3:]
        except:
            sys.stderr.write('\nUse: %s retain_pattern discard_pattern [files...]\n'%argv[0])
            sys.exit(1)
        retained_names = main(retain,discard,file_names)
        print(str(retained_names))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Thank you very much for the answer!

    I wish to know how to add an option which will choose to:
    1) print the relevant lines of the files for which the expressions were found.
    or
    2) if some -x paramter is added: only count the matches in the files only.

    Also I need to know how to add an option to generate html file from the output

    Thank you again for your help
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    gawk is easier for these tasks.
    My code processes the arguments on command line only. I now think you want to search the entire file.

    You'll have to build with pieces like these

    sys.argv : the command line parameters

    Code:
    inf = open('filename','r')
    i = 0
    for LINE in inf.readlines():
        i += 1  # line number
        LINE # line at line number
    regular_expresssion = re.compile('re')
    # the next item is True if and only if
    # the pattern is within the text
    regular_expression.search(LINE)
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Great, thanks!
    How do i decide either option (printing lines or counting matching only) based on some -x parameter in the command line?

    and how do I add the html file creation file in Python 2.6?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Study the unix tools. How can you combine gawk and wc to get the results you want? Learn to use the unix pipeline. Don't construct a monolithic program to solve all tasks. You're asking to reconstruct an already existing tool set.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo