为webkit的字符串设置source.qtvs。文件.read(),编码问题?

2024-04-25 05:46:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它将一堆JavaScript文件读入一个变量,然后将这些文件的内容放入Python模板中的占位符中。这将导致变量src(如下所述)的值是包含脚本的有效HTML文档。在

# 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 + '"""')

如果我试图通过Python中的PyQt5/QtWebKit打开它。。。在

^{pr2}$

…它不在web小部件中加载JS文件。我只剩下一页空白。在

但是如果我去掉了其他所有的东西,只写文件'"""src"""',当我在Chrome中打开这个文件时,它会按预期加载所有内容。同样,如果我从文件本身读取,它也会正确加载到web小部件中:

^{3}$

换句话说,当我运行这个脚本时,它会生成带有变量的Python输出文件;然后我尝试导入该变量并将其传递给webWidget.setHtml();但是页面不会呈现。但如果我使用open()并将其作为文件读取,它就会这样做。在

我怀疑这里有编码问题。我尝试过几种不同的变体。这些脚本都是UTF-8。在

有什么建议吗?非常感谢!在


Tags: 文件thesrc脚本内容readhtmljs