Converting strings escaped string literals or raw string

I recenly wrote a django app which needed to store regex strings in a model.  Since I couldn't find a way to store raw strings in models I wrote a function to convert unicode or string into string literals.

def to_raw_string(s):
        if isinstance(s,str):
           s = s.encode('string-escape')
        if isinstance(s,unicode):
           s = s.encode('unicode-escape')
        return s

Tagged as django , python raw string , rawstring string , unicode
Written by Andrew Konkol on July 24th, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.