如何对 html_string 进行 base64 编码

达西

目标

我正在尝试将foliumchoropleth编码StringIO. 我基于我对相关查询的回答我已经在这里这里检查了答案

错误

AttributeError: 'bytes' object has no attribute 'encode'

代码

视图.py

def get_choropleth(self, request):
    # make choropleth ('m')
    html_string = m.get_root().render()
    f = StringIO(html_string)
    choropleth = base64.b64decode(f.read())
    choropleth = choropleth.encode('utf8') # causing error
    return {'choropleth':choropleth}
达西

经过一些反复试验,以下对我有用:

解决方案

def get_choropleth(self, request):
        # make choropleth ('m')
        html_string = m.get_root().render()
        encoded_bytes = html_string.encode('utf-8')
        encoded_bytes = base64.b64encode(encoded_bytes)
        encoded_bytes = encoded_bytes.decode('utf8') # decode the b64 bytes for Unicode
        choropleth = encoded_bytes
        return {'choropleth':choropleth}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过命令行对base64进行编码?

如何对/ dev / random或/ dev / urandom进行base64编码?

C#如何对背景渐变进行base64编码

如何在 Jekyll 中进行 base64 编码

如何使用寓言(F#)对base64进行编码?

如何对JFrog Artifactory的密码进行base64编码?

如何在Kotlin中对IntArray进行Base64编码

如何以这种格式对 php 进行 base64 编码?

如何从base64编码的String重构音频Blob?

如何从HTML图像中获取base64编码的数据

如何在HTML中显示base64编码的图像

如何在Scala and Play Framework中对SHA-1哈希进行base64编码?

如何正确地对Gmail API进行base64编码MIMEText?

如何使用自定义字母集进行 base64 编码?

如何使用公钥加密数据并在python中使用base64对其进行编码?

如何在node.js中进行Base64编码?

如何在Linux bash / shell中对图像进行base64编码

如何使用公钥加密并使用 Javascript(NodeJS 等)使用 Base64 进行编码

如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码?

JAVA如何解密PHP对base64编码和RIJNDAEL_256编码的数据进行加密?

如何显示base64编码的pdf?

如何编码servlet对base64的响应?

如何在python中对64位二进制文件进行base64编码/解码?

如何将Map <String,String>编码为Base64字符串?

如何使用base64编码在html5中添加音频

Base64:如何编码/解码页面的整个HTML代码

在并集内部使用结构对base64进行编码

在js中对Videofile进行Base64编码

使用C和OpenSSL进行Base64编码