I currently got a program in Python that reads a text file but it loses its formatting while staying on memory for a couple of reasons, but it keeps as information the line and column of it. I would be interested on using this line and column information to reproduce the file as it was originally read. It is ok if the column doesn't match in amount of spaces or tabs in comparison to the original as long it is consistent thru the new file.
One first naive solution that occurred to me was to always keep some pointer to line 1 and column 1 and spam \n
and white spaces
using the line and column information, but I was wondering if there is a better way to do that in Python (in fact I don't know how to do this pointer to first line and column either).
Some method that would take as parameters a string, the line, column, and the file as four parameters in Python and would maybe be a possible solution, although I am unsure in this case what would occur if (line,column) is occupied (this would never occur in my situation so is not really a concern).
Edit: The information is stored on a complicated 'structure', but it suffices to say that I can extract such information as a list of strings, where each string has an associated line and column information. I would then use this 'method' to take each string and its column and line to add to the file on the right position.
Edit 2: The only assumption is that when getting every word from the original file they will happen on exactly the same order. That is to say, if the original file is "The cat jumped \n but did not die" then it is expected that I will be taken the strings: ' the', 'cat', 'jumped', 'and', 'didn't', 'die' and its associated line and columns. In that case, 'but', 'did', 'not' and 'die' will have line 2 instead of 1 and all words their associated columns (which may or may not overlap since its a different line).
Thank you.