Parsing strings (\n and \\)

Thomas Guettler zopestoller at thomas-guettler.de
Wed Jun 26 10:03:08 CEST 2002


Mark McEahern wrote:

>>Is there a function for parsing c-like
>>strings? I need to parse strings containing
>>'\n' and '\\' '\"'.
>>
> 
> I suppose it's obvious to anyone from a C background, but I find myself
> wondering, "What do you mean by parse?"
> 
> I might be able to help if you provide a little more detail.


Look at the two functoins quote and unquote. I wrote them
without regular expression because I think it faster.

def quote(string):
     '''
     foo\bar --> foo\\bar
     foo"bar --> foo\"bar
     '''

     new_string=[]
     for char in string:
         if char=='\\':
             new_string.append('\\\\')
         elif char=='"':
             new_string.append('\\"')
         else:
             new_string.append(char)
     return ''.join(new_string)

def unquote(string):
     '''
     foo\\bar --> foo\bar
     foo\"bar --> foo"bar
     '''
     new_string=[]
     i=0
     len_str=len(string)
     while 1:
         if i==len_str:
             break
         char=string[i]
         if char=='\\':
             i=i+1
             if i==len_str:
                 raise 'Parse Error: Backslash at end of string'
             char=string[i]
             if char=='\\':
                 new_string.append('\\')
             elif char=='"':
                 new_string.append('"')
             else:
                 raise 'Parse Error: Unsupported character after backslash'
         else:
             new_string.append(char)
         i=i+1
     return ''.join(new_string)

def test_quote():
     strings=['foo', '', '\\', ' ', '"', '\\"', '\\\\']
     for s in strings:
         if unquote(quote(s))!=s:
             print "Error: s: %s quoted: %s unquoted: %s" % (s, quote(s),
                                                             unquote(quote(s)))





More information about the Python-list mailing list