Python Vincent映射html输出不是有效的html
我正在用Python和Vincent编写一些代码来显示一些地图数据 文档中的示例如下所示:Python Vincent映射html输出不是有效的html,python,html,vincent,Python,Html,Vincent,我正在用Python和Vincent编写一些代码来显示一些地图数据 文档中的示例如下所示: import vincent county_topo = r'us_counties.topo.json' state_topo = r'us_states.topo.json' geo_data = [{'name': 'counties', 'url': county_topo, 'feature': 'us_counties.geo'},
import vincent
county_topo = r'us_counties.topo.json'
state_topo = r'us_states.topo.json'
geo_data = [{'name': 'counties',
'url': county_topo,
'feature': 'us_counties.geo'},
{'name': 'states',
'url': state_topo,
'feature': 'us_states.geo'}]
vis = vincent.Map(geo_data=geo_data, scale=3000, projection='albersUsa')
del vis.marks[1].properties.update
vis.marks[0].properties.update.fill.value = '#084081'
vis.marks[1].properties.enter.stroke.value = '#fff'
vis.marks[0].properties.enter.stroke.value = '#7bccc4'
vis.to_json('map.json', html_out=True, html_path='map_template.html')
运行此代码会输出一个html文件,但格式不正确。它是某种python字符串表示形式,b'..'
如果我删除引号和前导b,html页面在通过内置python服务器运行时将按预期工作
我的输出语句有什么问题?来自文档:
Python 2中忽略了前缀“b”或“b”;这表明
在Python 3中,文字应该变成字节文字(例如,当代码
自动转换为2to3)。“u”或“b”前缀可能是
后跟“r”前缀
您可以使用以下方法对其进行切片:
with open('map_template.html', 'w') a f:
html = f.read()[2:-1]
f.truncate()
f.write(html)
这将打开您的html
文件
b'<html><head><title>MyFile</title></head></html>'
b'MyFile'
并删除前2个字符和最后一个字符,这样您就可以:
<html><head><title>MyFile</title></head></html>
MyFile