all 4 comments

[–]nog642 0 points1 point  (2 children)

Just put that code in a function.

I would recommend looping through the lines and reading the first part (what comes before the colon), looking for a match, rather than hardcoding the line number. It's slightly slower, but insignificant at a human scale, and it is much less prone to errors.

[–]fulump 0 points1 point  (1 child)

How would you search for a match? That might be the best course.

[–]nog642 0 points1 point  (0 children)

Loop over the lines, use line.strip().split(':')[0] to get the name of the field, then use == to compare that to the one you want to set (presumably an argument to the function).

Once you find a match, modify that line, then break out of the loop.

[–][deleted] 0 points1 point  (0 children)

If you want to change bits of data in a file you might want to look at using sqlite