title: Setting HTML source for QtWebKit to a string value vs. file.read(), encoding issue?
body: <p>I have a script that reads a bunch of JavaScript files into a variable, and then places the contents of those files into placeholders in a Python template. This results in the value of the variable <code>src</code> (described below) being a valid HTML document including scripts.</p>
  
  <pre><code># Open the source HTML file to get the paths to the JavaScript files
  f = open(srcfile.html, 'rU')
  src = f.read()
  f.close()
  
  js_scripts = re.findall('script\ssrc="(.*)"', src)
  
  # Put all of the scripts in a variable
  js = ''
  for script in js_scripts:
      f = open(script, 'rU')
      js = js + f.read() + '\n'
      f.close()
  
  # Open/read the template
  template = open('template.html)
  templateSrc = Template(template.read())
  
  # Substitute the scripts for the placeholder variable
  src = str(templateSrc.safe_substitute(javascript_content=js))
  
  # Write a Python file containing the string
  with open('htmlSource.py', 'w') as f:
      f.write('#-*- coding: utf-8 -*-\n\nhtmlSrc = """' + src + '"""')
  </code></pre>
  
  <p>If I try to open it up via PyQt5/QtWebKit in Python...</p>
  
  <pre><code>from htmlSource import htmlSrc
  webWidget.setHtml(htmlSrc)
  </code></pre>
  
  <p>...it doesn't load the JS files in the web widget. I just end up with a blank page.</p>
  
  <p>But if I get rid of everything else, and just write to file <code>'"""src"""'</code>, when I open the file up in Chrome, it loads everything as expected. Likewise, it'll also load correctly in the web widget if I read from the file itself:</p>
  
  <pre><code>f = open('htmlSource.py', 'r')
  htmlSrc = f.read()
  webWidget.setHtml(htmlSrc)
  </code></pre>
  
  <p>In other words, when I run this script, it produces the Python output file with the variable; then I try to import that variable and pass it to <code>webWidget.setHtml()</code>; but the page doesn't render. But if I use <code>open()</code> and read it as a file, it does.</p>
  
  <p>I suspect there's an encoding issue going on here. But I've tried several variations of <code>encode</code> and <code>decode</code> without any luck. The scripts are all UTF-8.</p>
  
  <p>Any suggestions? Many thanks!</p>