Q:

python, search .txt file and inject character

I apologize for my ignorance of how python handles strings in advance. I have a .txt file that is at least 1000 lines long. It looks something like below
:dodge
1 6 some description string of unknown length
E7 8 another description string 
3445 0 oil temp something description voltage over limit etc

:ford
AF 4 description of stuff
0 8 string descritiopn

What I want to do is basically put a ";" before each string so what I will end up with is as follows
:dodge
1 6 ;some description string of unknown length
E7 8 ;another description string 
3445 0 ;oil temp something description voltage over limit etc

:ford
AF 4 ;description of stuff
0 8 ;string descritiopn

My idea is to open the file, search for ":" character, next line, goto " " character, goto next " " character and write a ";". Another thought was goto "/n" character in text file if next charachter != ":" then look for second space
    import sys
    import fileinput
with open("testDTC.txt", "r+") as f:
for line in f:
    if ' ' in line:     #read first space
        if ' ' in line:     #read second space
            line.append(';')

    f.write(line)

f.close()

I know its not close to getting what I need but its been a really long time since I did string manipulation in python. 

A:

You simply need to split  twice on whitespace and join the string, you don't need a regex for a simple repeating pattern:
with open("testDTC.txt") as f:
    for line in f:
        if line.strip() and not line.startswith(":"):
            spl = line.split(None,2)
            print("{} ;{}".format(" ".join(spl[:2]),spl[2]))

To write the changes to the original file you can use fileinput.input with inplace=True:
from fileinput import input
for line in input("testDTC.txt",inplace=True):
    if line.strip() and not line.startswith(":"):
        spl = line.split(None,2)
        print("{} ;{}".format(" ".join(spl[:2]),spl[2]),end="")
    else:
        print(line,end="")

Instead of indexing we can unpack:
        a, b, c = line.split(None,2)
        print("{} {} ;{}".format(a, b, c),end="")

Output:
:dodge
1 6 ;some description string of unknown length
E7 8 ;another description string 
3445 0 ;oil temp something description voltage over limit etc

:ford
AF 4 ;description of stuff
0 8 ;string descritiopn

For python 2 you can remove the end="" and use a commas after the print statement instead i.e print(line),
We avoid the starting paragraph lines with line.startswith(":") and  the empty lines with if line.strip().

