I have a list of tuples, each on contains a word-to-be-replaced, its line and column number positions from a given text file. I want to go through the text file and replace that specific word of that specific position with a character (e.g. [('word1', 1, 1), ('word2', 1, 9), ... ]
).
In other words, given a specific word, its line and column numbers inside a text file I am trying to find and replace that word with a character, for example:
given that the text file contains the following (assuming its position is as it is displayed -not written- here)
Excited him now natural saw passage offices you minuter. At by stack being court hopes. Farther so friends am to detract. Forbade concern do private be. Offending residence but men engrossed shy. Pretend am stack earnest arrived company so on. Felicity informed yet had to is admitted strictly how stack you.
and given that the word to replace is stack
with position in the text to be line 3
and column 16
, to replace it with the character *
,
so, after the replace takes place, the text file would now have the contents:
Excited him now natural saw passage offices you minuter. At by stack being court hopes. Farther so friends am to detract. Forbade concern do private be. Offending residence but men engrossed shy. Pretend am * earnest arrived company so on. Felicity informed yet had to is admitted strictly how stack you.
I have considered linecache but it seems very inefficient for large text files. Also, given the fact that I already have the line and column numbers, I hoped there was a way to go directly to that position and perform the replace.
Does anyone know a way to do this in Python?
EDIT
The initial solution proposed using numpy's genfromtxt
is (most likely) not suitable following the discussion in the follow-up issue since there is a need for every line of the text file to be present and not skipped (e.g. empty lines, strings beginning with 'w' and strings inside '/*.. /').