title: How do I preserve literals when passing from Python to C++ and back to Python
body: <p>Ok, here's the deal.  I have a program that makes a log in Python.  I wanted this log to be secure from edits when the program isn't using it, so I wrote a python script which creates C++ source designed to reprint the log back into Python.  Once I compile this source, I can have the log pretty secure, then just run the executable to retrieve it.  Its convoluted, but it works</p>
  
  <p><strong>The Issue:</strong> When I create the Python log, I replace <code>'</code> with the equivalent literal (<code>\'</code>) so that it doesn't ruin my strings while their stored.  But, when I pick this up with C++, and the plop it back down, I lose the literal, so I get broken strings.  Is there an easy way to replace the <code>'</code> in C++ with the corresponding literal similar to the <code>replace</code> function in Python.</p>
  
  <p><strong>Some code snippets that might help:</strong></p>
  
  <p>How I write string to the log in Python</p>
  
  <pre><code>logFile.write("    '{}'".format(somestring.replace("'","\\'").encode('ascii', 'ignore'))
  </code></pre>
  
  <p>What the Python log looks like:</p>
  
  <pre><code>CRDict = {"ID number string":[list of a bunch of items], "Another ID number: [Another list of things]}
  </code></pre>
  
  <p>How I write the C++ lines that store strings from the log in Python (where CR{} is <code>struct</code>)</p>
  
  <pre><code>CFile.write('    CR{}.somestring = "{}";\n'.format(num,somestring))
  </code></pre>
  
  <p>What the C++ line that stores the string ends up looking like</p>
  
  <pre><code>CR0.somestring = "This is a string and it doesn't keep track of literals";
  </code></pre>
  
  <p>The C++ line which writes the string back into Python (the file called <code>CRPYLog</code>)</p>
  
  <pre><code>CRPYLog &lt;&lt; "    '" + CR0.somestring + "'," &lt;&lt; endl;
  </code></pre>
  
  <p>And this is what the line looks like when it gets printed back into the Python log</p>
  
  <pre><code>'This is a string and it doesn't keep track of literals'
  </code></pre>
  
  <p>The above line is not syntactically valid, so it will break when I try to use the Python log</p>