Next / Previous / Contents / Shipman's homepage

10.3. uniQuotes(): Convert double-quotes to Unicode paired form

xnomo3.py
# - - -   u n i Q u o t e s

def uniQuotes(u):
    '''Convert Unicode double-quotes to the paired versions.

      [ u is a unicode string ->
          return u with even-numbered double-quote characters
          replaced by left double-quotes and odd-numbered ones
          by right double-quotes ]
    '''
    #-- 1 --
    quotes = False
    newList = []

    #-- 2 --
    for char in u:
        if char == u'"':
            if quotes: newList.append(UNI_RQ)
            else:      newList.append(UNI_LQ)
            quotes = not quotes
        else:
            newList.append(char)

    #-- 3 --
    if quotes:
        raise ValueError("Unbalanced quotes: %r" % u)
    else:
        return u''.join(newList)