pyzuki

grep_AW.py

Sep 11th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. # grep_AW.py
  2.  
  3. import re, sys
  4.  
  5. def urls(line):
  6.     if "}}}" in line:
  7.         idx = line.find('}}}')
  8.         line = line[:idx]
  9.         idx = line.rfind('http:')
  10.         url_ = line[idx:]
  11.         return url_
  12.     else:
  13.         return ""
  14.  
  15. def clean(line, chr_):
  16.     """
  17.    Cleans lines in RTF files such as those above. Heavy lifting is done when line has a URL.
  18.    If a line has a URL at the end, it first extracts the URL, cleans the line,
  19.    then returns the cleaned line with the URL replaced at the end.
  20.    
  21.    If there are one or more strings in line beginning with chr_ (e.g., '\\',
  22.    """
  23.     url_ = urls(line)
  24.     if '{' in line:
  25.         idx = line.find('{')
  26.         return line[:idx] + url_
  27.     n = None
  28.     while True:
  29.         if chr_ in line:
  30.             idx = line.find(chr_)
  31.             n = idx + 1
  32.             # return if end of line is reached
  33.             if n >= len(line):
  34.                 return line
  35.             while True:
  36.                 if n < len(line) and line[n] != ' ':
  37.                     str_ = line[idx:n]
  38.                 # remove str_ and return line + _url, if space char reached in line
  39.                 else:
  40.                     str_ = line[idx:n]
  41.                     line = line.replace(str_, "")
  42.                     return line + url_
  43.                 n += 1
  44.         else:
  45.             return line + url_
  46.        
  47. def clean_file(path):
  48.     f = open(path, 'r')
  49.     line_list = []
  50.     for line in f:
  51.         line_list.append(clean(line, '\\'))
  52.     f.close()
  53.     return line_list
  54.  
  55. def print_instructions():
  56.     print("Key (ignoring quotes): ' ' is a space, 'x' means any char except ' ' or '='")
  57.     print("If 1st 2 chars are:")
  58.     print("'=x' --> case insensitive, word")
  59.     print("' =' --> case sensitive, word")
  60.     print("' x' --> case sensitive, string")
  61.     print("'xx' --> case insensitive, string")
  62.     print("'==' --> start a new search of same file")
  63.  
  64. def print_searching(count):
  65.     if count == 0:
  66.         print("Searching", filename)
  67.     else:
  68.         print("Searching", filename, "within hits so far,")
  69.        
  70. def grep_line_list(line_list, line_list0, filename):
  71.     search = input("Enter search string or word; enter nothing to quit: ")
  72.     if search == '==':
  73.         line_list = line_list0
  74.         print()
  75.         print("Starting a fresh search of", filename)
  76.         search = input("Enter search string or word; enter nothing to quit: ")
  77.     if not search:
  78.         print("Bye")
  79.         sys.exit()
  80.     #'x': case insensitive
  81.     elif search[0] != ' ':
  82.         #'xx': case insensitive; not word
  83.         if search[0] != '=':
  84.             p = re.compile(search, re.I)
  85.         #'x=': case insensitive; word
  86.         else:
  87.             search = "\\b" + search[1:] + "\\b"
  88.             p = re.compile(search, re.I)
  89.     #' ': case sensitive
  90.     else:
  91.         #' x': case sensitive; not word
  92.         if search[1] != '=':
  93.             search = search[1:]
  94.             p = re.compile(search)
  95.         #' =': case sensitive; word
  96.         else:
  97.             search = search[2:]
  98.             search = "\\b" + search + "\\b"
  99.             p = re.compile(search)
  100.        
  101.     line_list2 = []
  102.     count = 0
  103.     for line in line_list:
  104.         if p.search(line):
  105.             line_list2.append(line)
  106.             count += 1
  107.     line_list = line_list2
  108.     return line_list, count
  109.    
  110. def print_results(line_list, count):
  111.     print("==================================")
  112.     print_count_only = input("print hits plus count? Enter nothing; print count only? Enter anything: ")
  113.  
  114.     if print_count_only == "":
  115.         print("Printing hits and count:")
  116.         line_list.sort()
  117.         for line in line_list:
  118.             print()
  119.             print(line)
  120.         print()
  121.         print("count =", count)
  122.     else:
  123.         print("Printing count only:")
  124.         print()
  125.         print("count =", count)
  126.     print()
  127.    
  128. def main():
  129.     count = 0
  130.     line_list0 = clean_file(path)
  131.     line_list = line_list0
  132.     print_instructions()
  133.     while True:
  134.         print_searching(count)
  135.         line_list, count = grep_line_list(line_list, line_list0, filename)
  136.         print()
  137.         print_results(line_list, count)
  138.        
  139. path = "C:\\Users\Dick\Desktop\Documents\\Notes\\ActiveWords.rtf"
  140. filename = 'ActiveWords.rtf'
  141.  
  142. if __name__ == '__main__':
  143.     main()
Advertisement
Add Comment
Please, Sign In to add comment