Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # grep_phone.py
- import re, sys
- #def get_last_path():
- #f = open(choice_path, 'r')
- #lines = []
- #for line in f:
- #lines.append(line)
- #f.close()
- #last_path = lines[0]
- #return last_path
- #def accept_last_choice(last_path):
- #print("Last choice was:", get_filename_from_path(last_path))
- #answer = input("If accept last choice, Enter nothing; if don't, enter anything: ")
- #return answer, last_path
- #def get_filename_from_path(path):
- #idx = path.rfind('\\')
- #filename = path[idx+1:]
- #return filename
- #def choose_file_to_grep(file_list, last_path, answer):
- #if answer == '':
- #return last_path
- #else:
- #print("file list:")
- #for i,x in enumerate(file_list):
- #print(i, get_filename_from_path(file_list[i]))
- #while True:
- #try:
- #idx = int(input("Enter index of file in file list: "))
- #if not 0 <= idx <= len(file_list)-1:
- #continue
- #except ValueError:
- #print('You did not enter an integer')
- #continue
- #return file_list[idx]
- def urls(line):
- if "}}}" in line:
- idx = line.find('}}}')
- line = line[:idx]
- idx = line.rfind('http:')
- url_ = line[idx:]
- return url_
- else:
- return ""
- def clean(line, chr_):
- """
- Cleans lines in RTF files such as those above. Heavy lifting is done when line has a URL.
- If a line has a URL at the end, it first extracts the URL, cleans the line,
- then returns the cleaned line with the URL replaced at the end.
- If there are one or more strings in line beginning with chr_ (e.g., '\\',
- """
- url_ = urls(line)
- if '{' in line:
- idx = line.find('{')
- return line[:idx] + url_
- n = None
- while True:
- if chr_ in line:
- idx = line.find(chr_)
- n = idx + 1
- # return if end of line is reached
- if n >= len(line):
- return line
- while True:
- if n < len(line) and line[n] != ' ':
- str_ = line[idx:n]
- # remove str_ and return line + _url, if space char reached in line
- else:
- str_ = line[idx:n]
- line = line.replace(str_, "")
- return line + url_
- n += 1
- else:
- return line + url_
- def clean_file(path):
- f = open(path, 'r')
- line_list = []
- for line in f:
- line_list.append(clean(line, '\\'))
- f.close()
- return line_list
- def print_instructions():
- print("Key (ignoring quotes): ' ' is a space, 'x' means any char except ' ' or '='")
- print("If 1st 2 chars are:")
- print("'=x' --> case insensitive, word")
- print("' =' --> case sensitive, word")
- print("' x' --> case sensitive, string")
- print("'xx' --> case insensitive, string")
- print("If search string = '==' --> start a new search of phone.rtf")
- #print("'==' --> start a new search of same file")
- def print_searching(count):
- if count == 0:
- print("Searching", filename)
- else:
- print("Searching", filename, "within hits so far,")
- def grep_line_list(line_list, line_list0, filename):
- search = input("Enter search string or word; enter nothing to quit: ")
- if search == '==':
- line_list = line_list0
- print()
- print("Starting a fresh search of", filename)
- search = input("Enter search string or word; enter nothing to quit: ")
- if not search:
- print("Bye")
- sys.exit()
- #'x': case insensitive
- elif search[0] != ' ':
- #'xx': case insensitive; not word
- if search[0] != '=':
- p = re.compile(search, re.I)
- #'x=': case insensitive; word
- else:
- search = "\\b" + search[1:] + "\\b"
- p = re.compile(search, re.I)
- #' ': case sensitive
- else:
- #' x': case sensitive; not word
- if search[1] != '=':
- search = search[1:]
- p = re.compile(search)
- #' =': case sensitive; word
- else:
- search = search[2:]
- search = "\\b" + search + "\\b"
- p = re.compile(search)
- line_list2 = []
- count = 0
- for line in line_list:
- if p.search(line):
- line_list2.append(line)
- count += 1
- line_list = line_list2
- return line_list, count
- def print_results(line_list, count):
- print("==================================")
- print_count_only = input("print hits plus count? Enter nothing; print count only? Enter anything: ")
- if print_count_only == "":
- print("Printing hits and count:")
- line_list.sort()
- for line in line_list:
- print()
- print(line)
- print()
- print("count =", count)
- else:
- print("Printing count only:")
- print()
- print("count =", count)
- print()
- def write_data(choice_path, data, mode):
- datafile = open(choice_path, mode)
- datafile.write(data)
- datafile.close()
- def main():
- count = 0
- line_list0 = clean_file(path)
- line_list = line_list0
- print_instructions()
- while True:
- print_searching(count)
- line_list, count = grep_line_list(line_list, line_list0, filename)
- print()
- print_results(line_list, count)
- #file_list = ['C:\\Users\Dick\Desktop\Documents\\ActiveWords.rtf',
- #'C:\\Users\Dick\Desktop\Documents\\ActiveWords_test.rtf',
- #'C:\\Users\Dick\Desktop\Documents\\phone.rtf',
- #'C:\\Users\Dick\Desktop\Documents\\phone_test.rtf',
- #'C:\\Users\Dick\Desktop\Documents\\test.rtf']
- path = 'C:\\Users\Dick\Desktop\Documents\\Notes\\phone.rtf'
- filename = 'phone.rtf'
- #choice_path = 'C:\\p31_data\choice.txt'
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment