Convert multiline string to single line string
I'm using Google App Engine and I need to put a multiline string in the datastore. Unfortunately, GAE does not allow that.开发者_StackOverflow社区 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?
You don't need no conversion:
google.appengine.ext.db.StringProperty(multiline=True)
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")
精彩评论