python - Modifying a file text -
i have text file this:
0 1 0 10.2 5.82 4.82 1 -1 0 8.21 5.74 3.62 0 1 1 5.33 8.66 5.47
this text file has few hundred rows pattern.
in first row, if first column 0, fourth column same. second column 1, fifth column has +10, value 15.82.
in second row, if first column 1, forth column has +10, value 18.21. second column -1, fifth column has -10, value -4.26. etc.
the final output this:
10.20 15.82 4.82 18.21 -4.26 3.62 5.33 18.66 15.47
i have tried using code wrote:
with open('abc') f, open('out.txt', 'w') f1: line in f: reff = line.split() if reff[0] == '0': value = float(reff[3]) = str(value) line = "".join(a) + " " + "".join(reff[4]) + " " + "".join(reff[5]) + '\n' elif reff[0] == '1': value = float(reff[3]) + 10 = str(value) line = "".join(a) + " " + "".join(reff[4]) + " " + "".join(reff[5]) + '\n' elif reff[0] == '-1': value = float(reff[3]) - 10 = str(value) line = "".join(a) + " " + "".join(reff[4]) + " " + "".join(reff[5]) + '\n' f1.write(line)
i have added more if
, elif
statements inside each if
, elif
statements in order check second column , third column. however, fourth column updated.
what doing wrong?
this can done simply:
with open('abc') f, open('out.txt', 'w') f1: line in f: line = line.split() in range(0,3): line[i+3] = float(line[i+3])+(int(line[i])*10) f1.write(' '.join([str(value) value in line[3:]]) + '\n')
which give output:
10.2 15.82 4.82 18.21 -4.26 3.62 5.33 18.66 15.47
Comments
Post a Comment