How do I change certain characters within a file using user input and loops?
TheFlooBearer (7)
I need to find a way to change the characters in the file that are integers into the variable that is inputted. Then, I need to have the program re-loop so that the user can re-change the variable as needed.

You are viewing a single comment. View All
mwilki7 (1079)

@TheFlooBearer sorry for the late reply but here is a sample snippet

2  filename = 'test.txt'                        # edit filename here
3  outputfile = filename                        # output overwrites original file
5  lines = open(filename).read().splitlines()   # put all lines in file into an 6  array
7  for i in range(len(lines)):                  # loop through the array
8      if lines[i] == "3":                      # if current line has a 3
9          lines[i] = "9999"                    # replace it with 9999
11 open(outputfile,'w').write('\n'.join(lines)) # write output

If you want to reloop, then wrap lines 4 thru 11 into a while loop (or for loop)