Strange error with text file editing in Python -
i'm using text file store weight of neural network i'm making, i'm having serious trouble editing weights stored in text field. essentially, making file regular format: word + \t + weight + \n, use follow code run through text file , grab parts:
with open(neuron_file, 'r+') original_neurons: 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 working, able change value of weight, , write same text file in same place. have managed create new file modified in way like, having strange problem writing original file. using below code it:
def replace(new_file, old_file): line in open(new_file): open(old_file, 'w').write(str(line)) but reason function breaks @ point in file. first 80% transfers fine, cuts file off @ seemingly random point in middle of line. ideas? know there other questions on similar topics, none of them seem applicable situation, , can't find mention of error 1 i'm getting.
problem navigable, primary interest in origin of error was. i've never seen , intrigued me, had no idea going on, hoping on here have more of idea.
with open('input.txt') in_file: open('output.txt', 'w') out_file: line in in_file.readlines(): word, weight = line.split()[:2] out_file.write('%s\t%s' % (word, float(weight) * 2)) with-block automaticaly closes opened files
Comments
Post a Comment