@@ -1153,8 +1153,8 @@ class AddCSVRowInputSpec(TraitedSpec):
1153
1153
field_headings = traits .List (traits .Str (), mandatory = True ,
1154
1154
desc = 'Heading list of available field to be added.' )
1155
1155
new_fields = traits .List ( traits .Any (), mandatory = True , desc = 'List of new values in row' , separator = ',' )
1156
- col_width = traits .Int ( 7 , mandatory = True , usedefault = True , desc = 'column width' )
1157
- float_dec = traits .Int ( 4 , mandatory = True , usedefault = True , desc = 'decimals' )
1156
+ col_width = traits .Int ( 9 , mandatory = True , usedefault = True , desc = 'column width' )
1157
+ float_dec = traits .Int ( 6 , mandatory = True , usedefault = True , desc = 'decimals' )
1158
1158
1159
1159
class AddCSVRowOutputSpec (TraitedSpec ):
1160
1160
csv_file = File (desc = 'Output CSV file containing rows ' )
@@ -1176,10 +1176,13 @@ class AddCSVRow(BaseInterface):
1176
1176
"""
1177
1177
input_spec = AddCSVRowInputSpec
1178
1178
output_spec = AddCSVRowOutputSpec
1179
+ _hdrstr = None
1179
1180
1180
1181
def _run_interface (self , runtime ):
1181
1182
cols = 0
1182
1183
headings = []
1184
+ col_width = self .inputs .col_width
1185
+ float_dec = self .inputs .float_dec
1183
1186
1184
1187
if not isdefined ( self .inputs .cols ) and not isdefined ( self .inputs .field_headings ):
1185
1188
iflogger .error ( 'Either number of cols or field headings is required' )
@@ -1205,42 +1208,45 @@ def _run_interface(self, runtime):
1205
1208
if len ( self .inputs .new_fields ) != cols :
1206
1209
iflogger .error ( 'Wrong length of fields, does not match number of cols' )
1207
1210
1208
- col_width = self .inputs .col_width
1209
- float_dec = self .inputs .float_dec
1211
+ if len (headings )> 0 :
1212
+ argstr = '{:>%d}' % col_width
1213
+ hdr = [ argstr .format ( '"' + h + '"' ) for h in self .inputs .field_headings ]
1214
+ self ._hdrstr = "," .join (hdr ) + '\n '
1210
1215
1211
- with open (self .inputs .in_file , 'a+' ) as in_file :
1212
- lines = in_file .readlines ()
1213
-
1214
- if len (lines )> 0 and lines [0 ]== '\n ' :
1215
- lines .pop ()
1216
1216
1217
- print len (lines )
1217
+ if op .exists ( self .inputs .in_file ):
1218
+ with open (self .inputs .in_file , 'a+' ) as in_file :
1219
+ lines = in_file .readlines ()
1218
1220
1219
- if (len (headings )> 0 ) and (len (lines )== 0 ):
1220
- hdr = [ '"%s"' % h for h in self .inputs .field_headings ]
1221
- hdrstr = "," .join (hdr )
1222
- lines = [ hdrstr + '\n ' ]
1223
-
1224
- if not lines [- 1 ] or lines [- 1 ]== '\n ' :
1221
+ if len (lines )> 0 and lines [0 ]== '\n ' :
1225
1222
lines .pop ()
1226
1223
1227
- row_data = []
1228
- metadata = dict (separator = lambda t : t is not None )
1229
- for name , spec in sorted (self .inputs .traits (** metadata ).items ()):
1230
- values = getattr (self .inputs , name )
1231
-
1232
- for v in values :
1233
- argstr = '{:>%d}' % col_width
1234
- if type (v )== 'float' :
1235
- argstr = '{:>%d.%df}' % ( col_width , float_dec )
1236
-
1237
- row_data .append ( argstr .format (v ) )
1238
-
1239
- newrow = "," .join ( row_data )
1240
- lines .append ( newrow + '\n ' )
1241
-
1242
- print "" .join ( lines )
1243
- in_file .write ( "" .join (lines ) )
1224
+ if (len (headings )> 0 ) and (len (lines )== 0 ):
1225
+ lines .insert (0 , self ._hdrstr )
1226
+ in_file .write ( "" .join (lines ) )
1227
+ else :
1228
+ with open (self .inputs .in_file , 'w+' ) as in_file :
1229
+ in_file .write ( self ._hdrstr )
1230
+
1231
+
1232
+ row_data = []
1233
+ metadata = dict (separator = lambda t : t is not None )
1234
+ for name , spec in sorted (self .inputs .traits (** metadata ).items ()):
1235
+ values = getattr (self .inputs , name )
1236
+ for v in values :
1237
+ argstr = '{:>%d}' % col_width
1238
+ if type (v ) is float :
1239
+ argstr = '{:>%d.%df}' % ( col_width , float_dec )
1240
+ if type (v ) is str :
1241
+ v = '"' + v + '"'
1242
+ row_data .append ( argstr .format (v ) )
1243
+ newrow = "," .join ( row_data ) + '\n '
1244
+
1245
+ with open (self .inputs .in_file , 'r+' ) as in_file :
1246
+ in_file .seek (- 2 , 2 )
1247
+ if in_file .read (2 ) == '\n \n ' :
1248
+ in_file .seek (- 1 , 1 )
1249
+ in_file .write ( newrow )
1244
1250
1245
1251
return runtime
1246
1252
0 commit comments