1
1
''' Module to read and write csv data with comments '''
2
2
3
- import sys , os , re
3
+ import sys
4
+ import os
5
+ import re
4
6
5
7
from datetime import datetime
6
8
from getpass import getuser
12
14
13
15
from hydrodiy import PYVERSION
14
16
15
- # Tailor string handling depending on python version
16
- if PYVERSION == 2 :
17
- from StringIO import StringIO
18
- UNICODE = unicode
19
-
20
- elif PYVERSION == 3 :
21
- from io import StringIO
22
- UNICODE = str
17
+ from io import StringIO
18
+ UNICODE = str
23
19
24
20
# Check distutils available for python folders
25
21
HAS_DISTUTILS = False
@@ -89,8 +85,8 @@ def _csvhead(nrow, ncol, comment, source_file, author=None):
89
85
head .append (f'# { key } : { comments [key ]} ' )
90
86
91
87
now = datetime .now ()
92
- head .append (' # time_generated : ' + \
93
- datetime .strftime (now , '%Y-%m-%d %H:%M:%S' ))
88
+ head .append (" # time_generated : "
89
+ + f" { datetime .strftime (now , '%Y-%m-%d %H:%M:%S' )} " )
94
90
95
91
# seek author
96
92
if author is None :
@@ -142,8 +138,8 @@ def _check_name(filename):
142
138
filename_full = '{0}.zip' .format (filename )
143
139
144
140
if not os .path .exists (filename_full ):
145
- raise ValueError ( ' Cannot find file {0 } (filename={1})' . format (\
146
- filename_full , filename ) )
141
+ errmess = f" Cannot find file { filename } ({ filename_full } )."
142
+ raise ValueError ( errmess )
147
143
148
144
return filename_full
149
145
@@ -153,31 +149,31 @@ def write2zip(archive, arcname, txt):
153
149
154
150
# Check file is not in the archive
155
151
zinfo_test = archive .NameToInfo .get (arcname )
156
- if not zinfo_test is None :
157
- raise ValueError (' File {0 } already exists in archive' . format ( arcname ) )
152
+ if zinfo_test is not None :
153
+ raise ValueError (f" File { arcname } already exists in archive" )
158
154
159
155
# Create fileinfo object
160
156
zinfo = zipfile .ZipInfo (arcname )
161
157
162
158
now = datetime .now ()
163
- zinfo .date_time = (now .year , now .month , now .day , \
164
- now .hour , now .minute , now .second )
159
+ zinfo .date_time = (now .year , now .month , now .day ,
160
+ now .hour , now .minute , now .second )
165
161
# File permission
166
- zinfo .external_attr = 33488896 # 0777 << 16
162
+ zinfo .external_attr = 33488896 # 0777 << 16
167
163
168
164
# Write to archive with header
169
- archive .writestr (zinfo , txt , \
170
- compress_type = zipfile .ZIP_DEFLATED )
171
-
172
-
173
- def write_csv (data , filename , comment ,\
174
- source_file ,\
175
- author = None ,\
176
- write_index = False ,\
177
- compress = True ,\
178
- float_format = '%0.5f' ,\
179
- archive = None ,\
180
- ** kwargs ):
165
+ archive .writestr (zinfo , txt ,
166
+ compress_type = zipfile .ZIP_DEFLATED )
167
+
168
+
169
+ def write_csv (data , filename , comment ,
170
+ source_file ,
171
+ author = None ,
172
+ write_index = False ,
173
+ compress = True ,
174
+ float_format = '%0.5f' ,
175
+ archive = None ,
176
+ ** kwargs ):
181
177
''' write a pandas dataframe to csv with comments in header
182
178
183
179
Parameters
@@ -218,16 +214,15 @@ def write_csv(data, filename, comment,\
218
214
source_file = str (source_file )
219
215
220
216
# Generate head
221
- head = _csvhead (data .shape [0 ], data .shape [1 ],\
222
- comment ,\
223
- source_file = source_file ,\
224
- author = author )
217
+ head = _csvhead (data .shape [0 ], data .shape [1 ], comment ,
218
+ source_file = source_file ,
219
+ author = author )
225
220
226
221
# Check source_file exists
227
222
if not os .path .exists (source_file ):
228
223
raise ValueError (source_file + ' file does not exists' )
229
224
230
- if not archive is None :
225
+ if archive is not None :
231
226
compress = False
232
227
233
228
# defines file name
@@ -249,9 +244,8 @@ def write_csv(data, filename, comment,\
249
244
fobj .write (line + '\n ' )
250
245
251
246
# Write data itself
252
- txt = data .to_csv (fobj , index = write_index ,\
253
- float_format = float_format , \
254
- ** kwargs )
247
+ txt = data .to_csv (fobj , index = write_index ,
248
+ float_format = float_format , ** kwargs )
255
249
256
250
# Store compressed data
257
251
if archive or compress :
@@ -261,9 +255,8 @@ def write_csv(data, filename, comment,\
261
255
arcname = filename
262
256
if compress :
263
257
arcname = os .path .basename (filename )
264
- archive = zipfile .ZipFile (filename_full , \
265
- mode = 'w' , \
266
- compression = zipfile .ZIP_DEFLATED )
258
+ archive = zipfile .ZipFile (filename_full , mode = 'w' ,
259
+ compression = zipfile .ZIP_DEFLATED )
267
260
268
261
write2zip (archive , arcname , txt )
269
262
@@ -274,8 +267,8 @@ def write_csv(data, filename, comment,\
274
267
fobj .close ()
275
268
276
269
277
- def read_csv (filename , has_colnames = True , archive = None , \
278
- encoding = 'utf-8' , ** kwargs ):
270
+ def read_csv (filename , has_colnames = True , archive = None ,
271
+ encoding = 'utf-8' , ** kwargs ):
279
272
''' Read a pandas dataframe from a csv with comments in header
280
273
281
274
Parameters
@@ -330,9 +323,9 @@ def read_csv(filename, has_colnames=True, archive=None, \
330
323
331
324
except TypeError :
332
325
import warnings
333
- warnings . warn (( ' Failed to open {0 } as a text file. ' + \
334
- ' Will now try to open it as a buffer' ). format ( filename ), \
335
- stacklevel = 2 )
326
+ warnmess = f" Failed to open { filename } as a text file." \
327
+ + " Will now try to open it as a buffer."
328
+ warnings . warn ( warnmess , stacklevel = 2 )
336
329
fobj = filename
337
330
338
331
else :
@@ -362,12 +355,12 @@ def read_csv(filename, has_colnames=True, archive=None, \
362
355
if 'names' not in kwargs :
363
356
# deals with multi-index columns
364
357
# reformat columns like (idx1-idx2-...)
365
- search = re .findall ('"\([^(]*\)"' , line )
358
+ search = re .findall ('"\\ ([^(]*\ \ )"' , line )
366
359
linecols = line
367
360
368
361
if search :
369
362
for cn in search :
370
- cn2 = re .sub ('\(| \)|"|\' ' , '' , cn )
363
+ cn2 = re .sub ('\\ (| \ \ )|"|\' ' , '' , cn )
371
364
cn2 = re .sub (', *' , '-' , cn2 )
372
365
linecols = re .sub (re .escape (cn ), cn2 , linecols )
373
366
@@ -377,18 +370,13 @@ def read_csv(filename, has_colnames=True, archive=None, \
377
370
kwargs .pop ('names' )
378
371
379
372
# Reads data with proper column names
380
- data = pd .read_csv (fobj , names = cns , \
381
- encoding = encoding , ** kwargs )
382
-
383
- data .columns = [re .sub ('\\ .' , '_' , cn )\
384
- for cn in data .columns ]
373
+ data = pd .read_csv (fobj , names = cns , encoding = encoding , ** kwargs )
374
+ data .columns = [re .sub ('\\ .' , '_' , cn ) for cn in data .columns ]
385
375
386
376
else :
387
377
# Reads data frame without header
388
- data = pd .read_csv (fobj , header = None , \
389
- encoding = encoding , ** kwargs )
378
+ data = pd .read_csv (fobj , header = None , encoding = encoding , ** kwargs )
390
379
391
380
fobj .close ()
392
381
393
382
return data , comment
394
-
0 commit comments