0

我正在用 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'},
            {'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'<html>....</html>'.

如果我删除引号和前导 b,则 html 页面在通过内置 python 服务器运行时会按预期工作。

我的输出语句有什么问题?

4

1 回答 1

0

从文档:

在 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>' 

并删除前 2 个和最后一个字符,为您提供:

    <html><head><title>MyFile</title></head></html>
于 2015-02-26T22:39:52.117 回答