Python Forum
Cloning a directory and using a .CSV file as a reference to search and replace - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Cloning a directory and using a .CSV file as a reference to search and replace (/thread-33828.html)



Cloning a directory and using a .CSV file as a reference to search and replace - bg25lam - May-31-2021

Hello I am trying to clone a directory with text files (XML, L5X, txt, etc). I would like to build something that would reference a .CSV with what text I would like to search for in column A and what I would like to replace that text with in column B. I would like my code to then copy all the text files in that folder and use the excel spreadsheet to make all the search and replacements of the file directory all in one pass instead of search and replace each file individually.

How would I go about doing this? What python methods are out there that would help with this? There's some material online that shows how to make changes to a single string of text but not much on mass search and replace referencing the csv/spreadsheet.

Any suggestions or reference code to do this to put me on the right track would be greatly appreciated!

Thank you


RE: Cloning a directory and using a .CSV file as a reference to search and replace - buran - May-31-2021

(May-31-2021, 04:37 AM)bg25lam Wrote: Any suggestions or reference code to do this to put me on the right track would be greatly appreciated!
https://docs.python.org/3/


RE: Cloning a directory and using a .CSV file as a reference to search and replace - bowlofred - May-31-2021

(May-31-2021, 04:37 AM)bg25lam Wrote: There's some material online that shows how to make changes to a single string of text but not much on mass search and replace referencing the csv/spreadsheet.

How many lines for things to search/replace and how big are the text files? For small numbers, just loop over everything. Is there any chance that a line may match two of the search lines? Will most lines have a change, or will most lines have no changes?

If there's not too many matches and not too many lines, just loop over everything. Build a dict where the keys are the search strings and the values are the replacement strings. For each line check each key and do a replace if it's there.

If the files aren't huge (GB), I'd be tempted to not break it into lines and instead just search/replace on the entire text. read() the file into a single string, search/replace it for each of the elements from the CSV, write the string back out.