Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2013, 12:55 PM
cardinalmanjune cardinalmanjune is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 cardinalmanjune User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 52 sec
Reputation 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

Reply With Quote
  #2  
Old February 3rd, 2013, 02:43 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old February 7th, 2013, 12:46 PM
cardinalmanjune cardinalmanjune is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 cardinalmanjune User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 52 sec
Reputation 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

Reply With Quote
  #4  
Old February 7th, 2013, 09:58 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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)

Reply With Quote
  #5  
Old February 7th, 2013, 10:05 PM
cardinalmanjune cardinalmanjune is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 cardinalmanjune User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 52 sec
Reputation 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?

Reply With Quote
  #6  
Old February 7th, 2013, 11:05 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Programming help in python 2.6

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap