1

I have a pretty large logfile (1.3G) which is more or less tabular with spaces padding the individual fields. I also have a list of indices in that file where I would like to overwrite a field's contents with some other string. For the sake of simplicity, assume that the field is previously empty and my replacement is not too long for the cell, so I only have to replace len(replacement) characters starting at index with replacement.

The number of replacements I need to perform in that file is around 10'000. How do I do this efficiently? Does Python have a data structure like a C array where I can just overwrite data?

8
  • 1
    You could use a bytearray, see stackoverflow.com/questions/10572624/mutable-strings-in-python (that would answer your literal question). However, it might be more "efficient" (depending on how you define that) to not load the entire file into memory but to read and write it line by line. Commented Sep 26, 2023 at 19:07
  • 1
    Perhaps take 2 or 3 lines from the log and show us the before and after. Commented Sep 26, 2023 at 19:09
  • 2
    Maybe also see stackoverflow.com/questions/508983/… Commented Sep 26, 2023 at 19:10
  • @BuzzMoschetti The real log is too complex to paste, but assume sth like: AAAAA BBBBB CCCCC\nAAAAA - DDDDD\nAAAAA EEEEE - \n and so on. Put simply, now have a list of all indices of - in the string (in reality it's not a simple dash but a complex regular expression with lookarounds) and want to paste XXnnn in the place of the dash, overwriting as many spaces that follow the dash as necessary. Commented Sep 26, 2023 at 19:25
  • 2
    @FelixDombek provide an example in the question itself. Give example inputs and example outputs. And I suppose, the point is you are trying to create a new file with the replacements? Commented Sep 26, 2023 at 19:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.