Created
February 23, 2016 20:19
Revisions
-
datamafia created this gist
Feb 23, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ def prepare_for_api(str_json): """ Prepare a json string API use by escaping double quotes and removing \n new line breaks :param str_json: string :return: string Note: Use pattern is to wrap data at the last possible moment to avoid extra escaping. """ str_json = re.sub(' +', ' ', str_json) # kills multi whitespace str_json = str_json.replace('\n', '') # kill new line breaks caused by triple quoted raw strings str_json = str_json.replace('"', '\\"') # address double quotes # Should be json ready. If not, add more. return str_json