Comment 23 for bug 710410

Revision history for this message
Michael Foord (mfoord) wrote :

One possible approach would be to store the repr of the string. This will escape quotes and so avoids the problem. You then use the "string_escape" codec to unescape again.

This would also escape newlines, so would turn strings into a single line. If you still want them to be human readable then you could manually replace "\n" with a newline in the escaped string and reverse this before unescaping.

e.g. to write:

    value = repr(the_real_value).replace('\\n', '\n')

and to read:

    the_real_value = value.replace('\n', '\\n').decode('string_escape')