Python Forum
Cloning a directory and using a .CSV file as a reference to search and replace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cloning a directory and using a .CSV file as a reference to search and replace
#1
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
Reply
#2
(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/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Beautiful Soup] Replace tag.string from another file? Winfried 0 89 11 hours ago
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 2,191 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,180 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Search in a file using regular expressions ADELE80 2 730 Dec-18-2024, 12:29 PM
Last Post: ADELE80
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,333 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 1,013 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,521 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Search Excel File with a list of values huzzug 4 2,851 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Replace a text/word in docx file using Python Devan 4 22,339 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 1,955 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020