I'm using a text file to store the weight of a neural network that I'm making, but I'm having serious trouble editing the weights stored in this text field. Essentially, I am making a file with a very regular format: Word + \t + Weight + \n, I then use the follow code to run through this text file and grab the parts:
with open(Neuron_File, 'r+') as Original_Neurons:
for Neuron in Original_Neurons:
Word_Stem = re.sub(r'^([a-z-]*)([\t]?)([0-9.]*)(\n)$', r'\1', Neuron)
Weight = float(re.sub(r'^([a-z-]*)([\t]?)([0-9.]*)(\n)$', r'\3', Neuron))
Which is working, however I would then like to be able to change the value of Weight, and write it back to the same text file in the same place. I have managed to successfully create a new file that is modified in the way that I would like, however I am having a strange problem with writing it back to the original file. I am using the below code for it:
def Replace(New_File, Old_File):
for line in open(New_File):
open(Old_File, 'w').write(str(line))
But for some reason this function simply breaks at a certain point in the file. The first 80% transfers fine, but then it cuts the file off at a seemingly random point in the middle of a line. Any ideas? I know there are other questions that are on similar topics, but none of them seem applicable to my situation, and I can't find any mention of another error like the one I'm getting.
Problem is navigable, but my primary interest is in what the origin of this error was. I've never seen anything like it and it intrigued me, as I had no idea what was going on, hoping someone on here would have more of an idea.