i will answer any questions i can
Basically I have a list of 70 words that I am looking for in over 500 files, and I need to replace them with new words and numbers.
ie... find "hello" and replace with "hello 233.4" but 70 words/numbers and 500+ files.
I found an informative post here, but I have been reading about sys.argv, re's, searches, replaces, etc.. etc.. etc.. I can not understand this bit of code. I have been "calling" (i think) it from "cmd" window on windows 7 with scriptname.py "-i " and "-o"...
if someone could put example input search list path "c:/input/file/path/searchlist.txt" and the example path for the file to be searched "c:/search/this/file/searchme.txt" in their correct positions please! (I will try and get it to repeat through every file in a folder on my own and highlight or bold the replacements on my own.)
I have tried many combinations... I could go over every modification ive made, and could type for days/pages/days/pages... each day/page getting dumber and dumber every time!
Thanks... OR IF YOU KNOW OF A DIFFERENT WAY, PLEASE SUGGEST ADVISE.
here is the link to the original post:
Use Python to search one .txt file for a list of words or phrases (and show the context)
here is the code from the original post:
import re
import sys
def main():
if len(sys.argv) != 3:
print("Usage: %s fileofstufftofind filetofinditin" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1]) as f:
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in f]
there = re.compile('|'.join(patterns))
with open(sys.argv[2]) as f:
for i, s in enumerate(f):
if there.search(s):
print("Line %s: %r" % (i, s))
main()