I am trying to read a source file (for example one in Python) and have each occurrence of a specific character to be replaced with 2 spaces and, also, count all such replaces.
From here, I use this
#!/usr/bin/env python3
import fileinput
with fileinput.FileInput(fileToSearch, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(specificChar, ' '), end='')
which works exactly as I want (even though it re-writes every single line).
But, I cannot find a way of counting the total number replaces; I though that trying
counter = 0
if is specificChar in line:
counter += 1
would not be efficient since I will then be traversing the line
once for the counter
and once for the line.replace
which seems not Pythonic to me.
Is there any other implementation I can consider?