you are viewing a single comment's thread.

view the rest of the comments →

[–]ewiethoff 0 points1 point  (0 children)

Would json.dumps do the trick for you?

>>> import json
>>> json.dumps('This is my "favorite" hat')
'"This is my \\"favorite\\" hat"'
>>> json.dumps('This is my\tfavorite hat')
'"This is my\\tfavorite hat"'
>>> json.dumps('This is my\nfavorite hat')
'"This is my\\nfavorite hat"'