aboutsummaryrefslogtreecommitdiff
path: root/qolab/file_utils
diff options
context:
space:
mode:
Diffstat (limited to 'qolab/file_utils')
-rw-r--r--qolab/file_utils/__init__.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/qolab/file_utils/__init__.py b/qolab/file_utils/__init__.py
index 67e64c8..af92c47 100644
--- a/qolab/file_utils/__init__.py
+++ b/qolab/file_utils/__init__.py
@@ -69,3 +69,19 @@ def get_next_data_file(prefix, savepath, run_number=None, date_format='%Y%m%d',
fname = os.path.join(savepath, f'{prefix}_{datestr}_f{run_number:05d}.{extention}')
return(fname)
+def save_table_with_header(fname, data, header='', comment_symbol='%', skip_headers_if_file_exist=False, item_format='e', item_separator='\t'):
+ # itemFormat examples: 'e', '.15e', 'f'
+ fname = filename2os_fname(fname)
+ file_exist_flag = os.path.exists(fname)
+ r, c = data.shape
+ item_format=str.join('', ['{', f':{item_format}', '}'])
+ with open(fname, 'a') as f:
+ if not skip_headers_if_file_exist:
+ for l in header:
+ f.write(f'{comment_symbol} {l}\n')
+ for r in data:
+ l=item_separator.join( map(item_format.format, r))
+ f.write(l)
+ f.write('\n')
+ f.close()
+