new style table
no header, comments given by '#': Very standard format, we just take care of multidimensional columns via special label notation
This commit is contained in:
parent
dd70b38d46
commit
757dc4e234
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from io import StringIO
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
|
@ -77,14 +77,21 @@ class Table():
|
||||||
f = fname
|
f = fname
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
|
||||||
header,keyword = f.readline().split()
|
try:
|
||||||
if keyword == 'header':
|
N_comment_lines,keyword = f.readline().split()
|
||||||
header = int(header)
|
if keyword != 'header':
|
||||||
else:
|
|
||||||
raise TypeError
|
raise TypeError
|
||||||
|
else:
|
||||||
comments = [f.readline()[:-1] for i in range(1,header)]
|
comments = [f.readline().strip() for i in range(1,int(N_comment_lines))]
|
||||||
labels = f.readline().split()
|
labels = f.readline().split()
|
||||||
|
except TypeError:
|
||||||
|
f.seek(0)
|
||||||
|
comments = []
|
||||||
|
line = f.readline().strip()
|
||||||
|
while line.startswith('#'):
|
||||||
|
comments.append(line.lstrip('#').lstrip())
|
||||||
|
line = f.readline().strip()
|
||||||
|
labels = line.split()
|
||||||
|
|
||||||
shapes = {}
|
shapes = {}
|
||||||
for label in labels:
|
for label in labels:
|
||||||
|
@ -313,7 +320,7 @@ class Table():
|
||||||
self.shapes[key] = other.shapes[key]
|
self.shapes[key] = other.shapes[key]
|
||||||
|
|
||||||
|
|
||||||
def to_ASCII(self,fname):
|
def to_ASCII(self,fname,new=False):
|
||||||
"""
|
"""
|
||||||
Store as plain text file.
|
Store as plain text file.
|
||||||
|
|
||||||
|
@ -335,13 +342,16 @@ class Table():
|
||||||
labels += ['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l) \
|
labels += ['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l) \
|
||||||
for i in range(np.prod(self.shapes[l]))]
|
for i in range(np.prod(self.shapes[l]))]
|
||||||
|
|
||||||
|
if new:
|
||||||
|
header = ['# {}'.format(comment) for comment in self.comments]
|
||||||
|
else:
|
||||||
header = ['{} header'.format(len(self.comments)+1)] \
|
header = ['{} header'.format(len(self.comments)+1)] \
|
||||||
+ self.comments \
|
+ self.comments \
|
||||||
+ [' '.join(labels)]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = open(fname,'w')
|
f = open(fname,'w')
|
||||||
except TypeError:
|
except TypeError:
|
||||||
f = fname
|
f = fname
|
||||||
for line in header: f.write(line+'\n')
|
|
||||||
|
for line in header + [' '.join(labels)]: f.write(line+'\n')
|
||||||
self.data.to_csv(f,sep=' ',index=False,header=False)
|
self.data.to_csv(f,sep=' ',index=False,header=False)
|
||||||
|
|
Loading…
Reference in New Issue