(row)
Heuristic to guess the top level delimiter.
Heuristic to guess the top level delimiter.
Heuristic to guess the top level delimiter .
def guess_delimiter(row):
    """Heuristic to guess the top level delimiter.
    """
    discarded  = set([
        '#', # this is for comments
        '_', # this is for spaces
        ' ', # spaces are not usually delimiter, unless we find no other
        '"', # this is for quoting
        '.', # this is for decimal numbers
        '@', # this is for duplicated keys
    ])
    candidates = set([l for l in row.rstrip() if not l.isalnum() and l not in discarded])
    counters   = dict((c, row.count(c)) for c in candidates)

    # Testing spaces from higher to lower, break on biggest match
    for alternate in [' ' * i for i in xrange(16, 3, -1)]:
        if row.count(alternate):
            counters[alternate] = row.count(alternate)
            break

    if counters:
        return max(counters.iteritems(), key=lambda x: x[1])[0]
    else:
        # In this case, we could not find any delimiter, we may
        # as well return ' '
        return ' '
def guess_delimiter ( row ) : discarded = set ( [ '#' , # this is for comments '_' , # this is for spaces ' ' , # spaces are not usually delimiter, unless we find no other '"' , # this is for quoting '.' , # this is for decimal numbers '@' , # this is for duplicated keys ] ) candidates = set ( [ l for l in row . rstrip ( ) if not l . isalnum ( ) and l not in discarded ] ) counters = dict ( ( c , row . count ( c ) ) for c in candidates ) # Testing spaces from higher to lower, break on biggest match for alternate in [ ' ' * i for i in xrange ( 16 , 3 , - 1 ) ] : if row . count ( alternate ) : counters [ alternate ] = row . count ( alternate ) break if counters : return max ( counters . iteritems ( ) , key = lambda x : x [ 1 ] ) [ 0 ] else : # In this case, we could not find any delimiter, we may # as well return ' ' return ' '