Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用';srcdoc&x27;属性具有Python中HTML文件的内容?(不使用';src';)_Javascript_Python_Html_Iframe - Fatal编程技术网

Javascript 如何使用';srcdoc&x27;属性具有Python中HTML文件的内容?(不使用';src';)

Javascript 如何使用';srcdoc&x27;属性具有Python中HTML文件的内容?(不使用';src';),javascript,python,html,iframe,Javascript,Python,Html,Iframe,以下是不起作用的函数: def合并html文件(输入文件路径、输出文件路径): html='\n\n\n' 对于i,枚举中的文件路径(输入文件路径): 文件=打开(文件路径'r') file_data=file.read() html+='{0}\n\n'.格式(i,文件\数据) html+='\n\n' 输出文件=打开(输出文件路径“w”) output_file.write(html) 输出_文件。关闭() 它将HTML文件列表合并到另一个HTML文件中,该文件的内容位于srcdoc属性中

以下是不起作用的函数:

def合并html文件(输入文件路径、输出文件路径):
html='\n\n\n'
对于i,枚举中的文件路径(输入文件路径):
文件=打开(文件路径'r')
file_data=file.read()
html+='{0}\n\n'.格式(i,文件\数据)
html+='\n\n'
输出文件=打开(输出文件路径“w”)
output_file.write(html)
输出_文件。关闭()
它将HTML文件列表合并到另一个HTML文件中,该文件的内容位于
srcdoc
属性中

我尝试过:

  • file\u data=json.dumps(file.read())
  • 同上:
    file\u data=json.dumps(file.read())
    并删除
    srcdoc
    value
    srcdoc={1}
作为参考,这个简单的静态HTML文件在
srcdoc
中只包含1个元素:


0
1.
但是,有一次我尝试了一个更典型的HTML文件,比如作为
srcdoc
的值:


函数初始化(){
var map=new google.maps.map(document.getElementById(“map_canvas”){
缩放:10,
中心:新google.maps.LatLng(1.337046103.892634)
});
新google.maps.Circle({
strokeColor:“#F0F8FF”,
笔划不透明度:1.0,
冲程重量:1,
填充颜色:'#F0F8FF',
});
}
…然后它就失败了。加载的页面只显示空白矩形。
我相信这个问题是由于字符串的编码。使用
json.dumps
方法在引号前插入转义字符。但是,它仍然不起作用。

来晚了,但希望这对其他人有帮助

正如您所提到的,这确实是一个与字符串格式有关的问题。在Python中表示字符串时使用的尾随字符\n或'

在您的情况下,可以使用html Python库转义这些字符,方法如下:

clean_html = html.escape(file_data)
将在此处插入:

...
file_data = file.read()
clean_html = html.escape(file_data)
html += '<h3>{0}</h3>\n<iframe id="{0}" srcdoc="{1}" width="810" height="260" frameborder="0"></iframe>\n'.format(i, clean_html )
...
。。。
file_data=file.read()
clean\u html=html.escape(文件\u数据)
html+='{0}\n\n'.格式(i,clean_html)
...

请注意,在这种情况下,您必须更改名为html的变量,因为您使用的html包晚到了这里,但希望这对其他人有所帮助

正如您所提到的,这确实是一个与字符串格式有关的问题。在Python中表示字符串时使用的尾随字符\n或'

在您的情况下,可以使用html Python库转义这些字符,方法如下:

clean_html = html.escape(file_data)
将在此处插入:

...
file_data = file.read()
clean_html = html.escape(file_data)
html += '<h3>{0}</h3>\n<iframe id="{0}" srcdoc="{1}" width="810" height="260" frameborder="0"></iframe>\n'.format(i, clean_html )
...
。。。
file_data=file.read()
clean\u html=html.escape(文件\u数据)
html+='{0}\n\n'.格式(i,clean_html)
...
请注意,在这种情况下,您必须更改名为html的变量,因为您使用的是html包