-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_convert_unmodified.py
38 lines (25 loc) · 1.06 KB
/
data_convert_unmodified.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import argparse
parser = argparse.ArgumentParser(description='Read a file with 3 space separated columns, calculate an error estimate from the second column and write the modified file.')
parser.add_argument('input', help='Name of the input file.')
parser.add_argument('output', help='Name of the output file.')
arguments = parser.parse_args()
x_data = []
y_data = []
e_data = []
infile = open(arguments.input, 'r')
# Go through the file line by line
for line in infile:
# Remove whitespace characters at the beginning and end
stripped_line = line.strip()
# If it's not a comment and not an empty line, try to convert it to three floats.
if not stripped_line.startswith('#') and not len(stripped_line) == 0:
x, y, e = [float(x) for x in line.split(' ')]
x_data.append(x)
y_data.append(y)
e_data.append(e)
infile.close()
lineformat = '{:.6g} {:.6g} {:.6g}\n'
outfile = open(arguments.output, 'w')
for x, y, e in zip(x_data, y_data, e_data):
outfile.write(lineformat.format(x, y, e))
outfile.close()