-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_tools.py
executable file
·74 lines (58 loc) · 2 KB
/
file_tools.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys,os
def file_read (file):
"""Strip lines of ASCII text from file"""
print ('Reading '+file+'....')
f=open(file, 'r')
block_lines = f.read()
f.close() #close the file
#print block_lines
eol = os.linesep
lines_of_file = block_lines.split('\n') #divide into lines at carriage rtns
del lines_of_file [0] # header
del lines_of_file [-1] # last line is blank so remove it
print lines_of_file [0]
return lines_of_file
def file_write (file,data):
"""Write lines of data to ASCII file"""
f=open(file, "w")
for each_line in data:
joined_line = '\t'.join(each_line) # \t for tab
f.write(joined_line) # Write the isochronous line.
f.write('\r') # write a carriage return what a nasty hack!
f.close()
return
def addFilenamePrefix (path, prefix=""):
#take a path and add a prefix to the filename (for example, datestamp)
directory, filename = os.path.split(path)
filename = prefix + filename
return os.path.join(directory,filename)
def lines_into_traces (lines):
"""Convert a list of ASCII text lines into traces (a list of lists of floats)"""
#print lines
split_lines=[]
for line in lines:
values=line.split('\t') #divide lines into values at tabs
split_lines.append(values)
#print split_lines
traces = []
num_of_traces = len(split_lines[0]) #work out how many traces from the no of columns
## make an empty list
for i in range(num_of_traces):
traces.append([])
## transpose lines into traces made from columns
for line in split_lines:
for i in range (num_of_traces):
traces[i].append (float(line[i]))
return traces
def traces_into_lines (traces):
"""Convert traces (a list of lists of floats) into a list of ASCII text lines"""
lines_of_output=len(traces[0])
## make an empty list
lines = []
for i in range(lines_of_output):
lines.append([])
## transpose the traces into lines
for point in range(0,lines_of_output):
for trace in traces:
lines[point].append(str(trace[point]))
return lines