title: python: write 'backslash double-quote' string into file
body: <p>I've got an input file with some string containing double quotes in it, and want to generate a C-style header file with Python.</p>
  
  <p>Say,</p>
  
  <pre><code>input file: Hello "Bob"
  output file: Hello \"Bob\"
  </code></pre>
  
  <p>I can't write the code to obtain such a file, here's what I've tried so far:</p>
  
  <pre><code>key = 'key'
  val = 'Hello "Bob"'
  buf_list = list()
  ...
  val = val.replace('"', b'\x5c\x22')
  # also tried: val = val.replace('"', r'\"')
  # also tried: val = val.replace('"', '\\"')
  buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
  ...
  for keyval in buf_list:
      lang_file.write(keyval)
  lang_file.close()
  </code></pre>
  
  <p>The output file always contains:</p>
  
  <pre><code>Hello \\\"Bob\\\"
  </code></pre>
  
  <p>I had no problems writing <code>\n</code>, <code>\t</code> strings into the output file.</p>
  
  <p>It seems I can only write zero or two backslashes, can someone help please ?</p>