python - Read a complete data file and round numbers to 2 decimal places and save it with the same format -
i trying learn python , have intention make big data file smaller , later statistical analysis r. need read data file (see below):
scalar nd 3 st 0 ts 10.00 0.0000 0.0000 0.0000 scalar nd 3 st 0 ts 3600.47 255.1744 255.0201 255.2748 scalar nd 3 st 0 ts 7200.42 255.5984 255.4946 255.7014
and find numbers , round in 2 digits after decimal, svae maximum number namber in front of ts. @ end save data file same format following:
scalar nd 3 st 0 ts 10.00 0.00 0.00 0.00 scalar nd 3 st 0 ts 3600.47 255.17 255.02 255.27 scalar nd 3 st 0 ts**max** 7200.42 255.60 255.49 255.70
i have written code this:
import numpy np import matplotlib.pyplot plt import pickle # open file f = open('data.txt', 'r') thefile = open('output.txt', 'wb') # read , ignore header lines header1 = f.readline() header2 = f.readline() header3 = f.readline() header4 = f.readline() data = [] line in f: line = line.strip() columns = line.split() source = {} source['wsp'] = columns[0] #source['timestep'] = float(columns[1]) source['timestep'] = columns[1] data.append(source) f.close()
but number in front of ts cannot read. wanted round numbers float used not work. after wanted put in loop suggestion, write code in way? thankfull help.
the first part can done if can assumed floats require rounding occur on lines themselves. excludes lines prefixed alpha chars, e.g. ts 3600.47
.
from __future__ import print_function open('data.txt') f, open('output.txt', 'w') outfile: line in (l.rstrip() l in f): try: print('{:.2f}'.format(float(line)), file=outfile) except valueerror: print(line, file=outfile)
the second part, however, requires file buffered in entirety because not known maximum value ts
- @ start of file, @ end, or anywhere in between. here's code that:
from __future__ import print_function open('data.txt') f, open('output.txt', 'w') outfile: lines = [] max_ts = 0 max_ts_idx = none i, line in enumerate(l.rstrip() l in f): try: lines.append('{:.2f}'.format(float(line))) except valueerror: if line.startswith('ts'): new_ts = float(line.split()[-1]) if new_ts > max_ts: max_ts = new_ts max_ts_idx = lines.append(line) i, line in enumerate(lines): if == max_ts_idx: line = line.replace('ts', 'ts**max**') print(line, file=outfile)
it's same print version above, however, lines accumulated list lines
. maximum value "ts" lines kept in max_ts
, corresponding line number of "ts" line in max_ts_idx
. lines
list iterated on , lines written file. if line contains maximum value "ts" (as determined max_ts_idx
) line decorated **max**
.
Comments
Post a Comment