Revision 14600
Added by Aaron Marcuse-Kubitza over 10 years ago
trunk/lib/csvs.py | ||
---|---|---|
270 | 270 |
pairs += sorted(dict_.items()) # then remaining cols in alphabetical order |
271 | 271 |
return (dicts.pair_keys(pairs), dicts.pair_values(pairs)) |
272 | 272 |
|
273 |
class JsonReader(Filter):
|
|
274 |
'''reads parsed JSON data as row tuples
|
|
275 |
@param json_data [{'col': 'value', __}, __]
|
|
273 |
class row_dict_to_list_reader(Filter):
|
|
274 |
'''reads dict-based rows as list-based rows
|
|
275 |
@param reader [{'col': 'value', __}, __]
|
|
276 | 276 |
''' |
277 |
def __init__(self, json_data, col_order=[]):
|
|
277 |
def __init__(self, reader, col_order=[]):
|
|
278 | 278 |
self.header = None |
279 | 279 |
|
280 | 280 |
def filter_(row_dict): |
281 | 281 |
header, row = row_dict_to_list(row_dict, col_order) |
282 | 282 |
|
283 |
if self.header == None: # 1st JSON row: header
|
|
283 |
if self.header == None: # 1st dict row: header
|
|
284 | 284 |
self.header = header |
285 | 285 |
self.next_row = row |
286 | 286 |
row = header |
287 |
elif self.next_row != None: # 1st JSON row: data
|
|
287 |
elif self.next_row != None: # 1st dict row: data
|
|
288 | 288 |
row = self.next_row |
289 | 289 |
self.next_row = None |
290 |
else: # remaining JSON rows
|
|
290 |
else: # remaining dict rows
|
|
291 | 291 |
assert header == self.header # all rows must have same cols |
292 | 292 |
|
293 | 293 |
return row |
294 |
Filter.__init__(self, filter_, iter(json_data))
|
|
294 |
Filter.__init__(self, filter_, reader)
|
|
295 | 295 |
self.next_row = None |
296 |
|
|
297 |
class JsonReader(MultiFilter): |
|
298 |
'''reads parsed JSON data as row tuples |
|
299 |
@param json_data [{'col': 'value', __}, __] |
|
300 |
''' |
|
301 |
def __init__(self, json_data, col_order=[]): |
|
302 |
def preprocess(json_row): return json_row |
|
303 |
MultiFilter.__init__(self, row_dict_to_list_reader(Filter(preprocess, |
|
304 |
iter(json_data)))) |
Also available in: Unified diff
lib/csvs.py: JsonReader: factored out row-dict-to-list into new row_dict_to_list_reader so that JSON-specific preprocessing is kept separate from the row format translation