invalid characters happens by using truncate(0) in python

I used truncate(0) in python to overwrite data on my file. Part of the code is like this: l=f.readlines() f.truncate(0) for w in l: data=w.split() if 105==int(data[0]): f.write(‘%s %s\n’%((str(data[0]),str(np.mean(b))))) else: f.write(‘%s %s\n’%((str(data[0]),(data[1])))) The code works fine and output is correct, but when I open the output file (which is in txt format) I got "invalid… Read More invalid characters happens by using truncate(0) in python