views:

100

answers:

2

Hi,

I'm using Google App Engine and I need to put a multiline string in the datastore. Unfortunately, GAE does not allow that. I need this string to be multiline, so is there any way to convert a multiline string to a single line string and store it?

A: 

Replace all newlines with "\n", and replace all "\" with "\\", just like the way you do with string literals:

def encode(s):
    return s.replace("\\", "\\\\").replace("\n", "\\n")
def decode(s):
    return s.replace("\\\\", "\\").replace("\\n", "\n")
Marvin
Do as I say, not as I code. There is a bug in the code, but the principle is sound.
Marvin
+4  A: 

You don't need no conversion:

google.appengine.ext.db.StringProperty(multiline=True)

msw
Sweet, thanks a lot!
Haha, turned out my string was too many kb anyway, so I made it a TextProperty.