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
  Search Excel File with a list of values huzzug 4 1,212 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Replace a text/word in docx file using Python Devan 4 3,227 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 751 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,307 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,270 Jun-27-2023, 01:17 PM
Last Post: diver999
  search file by regex SamLiu 1 902 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 972 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,105 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,531 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
  Replace columns indexes reading a XSLX file Larry1888 2 975 Nov-18-2022, 10:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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