Project

General

Profile

« Previous | Next » 

Revision 14594

lib/csvs.py: added row_dict_to_list(), which translates a CSV dict-based row to a list-based one

View differences:

trunk/lib/csvs.py
4 4
import _csv
5 5
import StringIO
6 6

  
7
import dicts
7 8
import exc
8 9
import lists
9 10
import streams
......
249 250
            raise
250 251
    
251 252
    def read(self, n): return self.readline() # forward all reads to readline()
253

  
254
def row_dict_to_list(dict_, col_order=[]):
255
    '''translates a CSV dict-based row to a list-based one
256
    @param dict_ {'col': 'value', __}
257
    @return (header, row) = (['col', __], ['value', __])
258
    '''
259
    dict_ = dict_.copy() # don't modify input!
260
    pairs = []
261
    for col in col_order: pairs.append((col, dict_.pop(col))) # ordered cols 1st
262
    pairs += sorted(dict_.items()) # then remaining cols in alphabetical order
263
    return (dicts.pair_keys(pairs), dicts.pair_values(pairs))

Also available in: Unified diff