@@ -151,13 +151,15 @@ def __init__(self, filename,
151
151
# An entry for each column in the file and compiled by _handle_header
152
152
# using the provided header or file content oin the first iteration.
153
153
self ._converters = []
154
+ # The the column names extracted from the header
155
+ self ._column_names = []
154
156
155
157
def __iter__ (self ):
156
- """Return the next typ -converted row from the file.
158
+ """Return the next type -converted row from the file.
157
159
The first row is expected to be a header with optional
158
160
type definitions.
159
161
160
- :returns: A list of type-converted values for the next row
162
+ :returns: A dictionary of type-converted values for the next row
161
163
162
164
:raises: ValueError if a column value cannot be converted
163
165
:raises: ContentError if the column value is unknown or does not
@@ -183,10 +185,10 @@ def __iter__(self):
183
185
if len (self ._converters ) == 0 :
184
186
raise ContentError (1 , 1 , None , 'Missing header' )
185
187
186
- # Construct a list of row column values,
188
+ # Construct a dictionary of row column names and values,
187
189
# applying type conversions based on the
188
190
# type defined in the header....
189
- row_values = []
191
+ row_content = {}
190
192
col_index = 0
191
193
# Convert...
192
194
for col in row :
@@ -195,14 +197,15 @@ def __iter__(self):
195
197
raise ContentError (col_index + 1 , self ._c_reader .line_num ,
196
198
None , 'Too many values' )
197
199
try :
198
- row_values .append (self ._converters [col_index ][1 ](col ))
200
+ row_content [self ._column_names [col_index ]] = \
201
+ self ._converters [col_index ][1 ](col )
199
202
except ValueError :
200
203
raise ContentError (col_index + 1 , self ._c_reader .line_num ,
201
204
col ,
202
205
'Does not comply with column type' )
203
206
col_index += 1
204
207
205
- yield row_values
208
+ yield row_content
206
209
207
210
def _handle_hdr (self , hdr ):
208
211
"""Given the file header line (or one provided when the class
@@ -218,7 +221,10 @@ def _handle_hdr(self, hdr):
218
221
if len (cell_parts ) not in [1 , 2 ]:
219
222
raise ContentError (column_number , self ._c_reader .line_num ,
220
223
cell , 'Expected name and type (up to 2 items)' )
221
- name = cell_parts [0 ]
224
+ name = cell_parts [0 ].strip ()
225
+ if len (name ) == 0 :
226
+ raise ContentError (column_number , self ._c_reader .line_num ,
227
+ cell , 'Column name is empty' )
222
228
if len (cell_parts ) == 2 :
223
229
column_type = cell_parts [1 ].lower ()
224
230
if column_type not in CONVERTERS :
@@ -227,6 +233,7 @@ def _handle_hdr(self, hdr):
227
233
# Unspecified - assume built-in 'string'
228
234
column_type = 'string'
229
235
self ._converters .append ([name , CONVERTERS [column_type ]])
236
+ self ._column_names .append (name )
230
237
column_number += 1
231
238
232
239
def __del__ (self ):
0 commit comments