import json as pyjson values = [ ('mixture_excess_term.json','../../CoolProp/mixture_excess_JSON.h','mixture_excess_JSON'), ('mixture_reducing_parameters.json','../../CoolProp/mixture_reducing_JSON.h','mixture_reducing_JSON') ] for infile,outfile,variable in values: # Check you haven't messed up the JSON file and it will still load pyjson.loads(open(infile,'r').read()) json = open(infile,'r').read() # Escape all existing quotations json = json.replace('"','\\"') # Split into lines json = json.split('\n') # Add " to beginning and end of every line and end line with comma for i,line in enumerate(json): json[i] = '"'+line+'",' # Back together json = '\n'.join(json) # Header/footer json = '// File generated by the script dev/mixtures/JSON_to_C++.py\n\n// C array of std::string lines\nstd::string '+variable+'_c_lines[] = {"",\n' + json + '};'+'\n\n// Combined into a single std::string \nstd::vector '+variable+'_lines('+variable+'_c_lines, '+variable+'_c_lines + sizeof('+variable+'_c_lines) / sizeof(std::string)); \n\nstd::string '+variable+' = strjoin('+variable+'_lines,"");' f = open(outfile,'w') print>>f, json