Skip to content

Commit b558c8f

Browse files
committed
feature: can skip sys comments in csv
1 parent 27fbbc0 commit b558c8f

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

hydrodiy/io/csv.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def _header2comment(header):
5656
return comment
5757

5858

59-
def _csvhead(nrow, ncol, comment, source_file, author=None):
59+
def _csvhead(nrow, ncol, comment, source_file, author=None, \
60+
add_sys_comments=True):
6061
""" Produces a nice header for csv files """
6162

6263
# Generate the comments dict
@@ -85,13 +86,6 @@ def _csvhead(nrow, ncol, comment, source_file, author=None):
8586
head.append(f'# nrow : {nrow}')
8687
head.append(f'# ncol : {ncol}')
8788

88-
for key in sorted(comments):
89-
head.append(f'# {key} : {comments[key]}')
90-
91-
now = datetime.now()
92-
head.append('# time_generated : ' + \
93-
datetime.strftime(now, '%Y-%m-%d %H:%M:%S'))
94-
9589
# seek author
9690
if author is None:
9791
try:
@@ -101,20 +95,29 @@ def _csvhead(nrow, ncol, comment, source_file, author=None):
10195

10296
head.append('# author : ' + author)
10397

104-
# seek source file
105-
head.append(f'# source_file : {source_file}')
98+
# Add comments
99+
for key in sorted(comments):
100+
head.append(f'# {key} : {comments[key]}')
101+
102+
now = datetime.now()
103+
head.append('# time_generated : ' + \
104+
datetime.strftime(now, '%Y-%m-%d %H:%M:%S'))
105+
106+
if add_sys_comments:
107+
# seek source file
108+
head.append(f'# source_file : {source_file}')
106109

107-
# Python config
108-
head.append(f'# work_dir : {os.getcwd()}')
109-
head.append(f'# python_environment {os.name}')
110-
version = sys.version.replace("\n", " ")
111-
head.append(f'# python_version : {version}')
112-
head.append(f'# pandas_version : {pd.__version__}')
113-
head.append(f'# numpy_version : {np.__version__}')
110+
# Python config
111+
head.append(f'# work_dir : {os.getcwd()}')
112+
head.append(f'# python_environment {os.name}')
113+
version = sys.version.replace("\n", " ")
114+
head.append(f'# python_version : {version}')
115+
head.append(f'# pandas_version : {pd.__version__}')
116+
head.append(f'# numpy_version : {np.__version__}')
114117

115-
if HAS_DISTUTILS:
116-
head.append('# python_inc : ' + get_python_inc())
117-
head.append('# python_lib : ' + get_python_lib())
118+
if HAS_DISTUTILS:
119+
head.append('# python_inc : ' + get_python_inc())
120+
head.append('# python_lib : ' + get_python_lib())
118121

119122
head.append('# --------------------------------------------------')
120123

@@ -177,6 +180,7 @@ def write_csv(data, filename, comment,\
177180
compress=True,\
178181
float_format='%0.5f',\
179182
archive=None,\
183+
add_sys_comments=True, \
180184
**kwargs):
181185
''' write a pandas dataframe to csv with comments in header
182186
@@ -221,7 +225,8 @@ def write_csv(data, filename, comment,\
221225
head = _csvhead(data.shape[0], data.shape[1],\
222226
comment,\
223227
source_file=source_file,\
224-
author=author)
228+
author=author, \
229+
add_sys_comments=add_sys_comments)
225230

226231
# Check source_file exists
227232
if not os.path.exists(source_file):

0 commit comments

Comments
 (0)