<i id='f1IRf'><tr id='f1IRf'><dt id='f1IRf'><q id='f1IRf'><span id='f1IRf'><b id='f1IRf'><form id='f1IRf'><ins id='f1IRf'></ins><ul id='f1IRf'></ul><sub id='f1IRf'></sub></form><legend id='f1IRf'></legend><bdo id='f1IRf'><pre id='f1IRf'><center id='f1IRf'></center></pre></bdo></b><th id='f1IRf'></th></span></q></dt></tr></i><div id='f1IRf'><tfoot id='f1IRf'></tfoot><dl id='f1IRf'><fieldset id='f1IRf'></fieldset></dl></div>

        <bdo id='f1IRf'></bdo><ul id='f1IRf'></ul>

      1. <small id='f1IRf'></small><noframes id='f1IRf'>

      2. <tfoot id='f1IRf'></tfoot>
        <legend id='f1IRf'><style id='f1IRf'><dir id='f1IRf'><q id='f1IRf'></q></dir></style></legend>

      3. Base 64在Python中对JSON变量进行编码

        Base 64 encode a JSON variable in Python(Base 64在Python中对JSON变量进行编码)
        <i id='DOw3L'><tr id='DOw3L'><dt id='DOw3L'><q id='DOw3L'><span id='DOw3L'><b id='DOw3L'><form id='DOw3L'><ins id='DOw3L'></ins><ul id='DOw3L'></ul><sub id='DOw3L'></sub></form><legend id='DOw3L'></legend><bdo id='DOw3L'><pre id='DOw3L'><center id='DOw3L'></center></pre></bdo></b><th id='DOw3L'></th></span></q></dt></tr></i><div id='DOw3L'><tfoot id='DOw3L'></tfoot><dl id='DOw3L'><fieldset id='DOw3L'></fieldset></dl></div>

            <tfoot id='DOw3L'></tfoot>

            <small id='DOw3L'></small><noframes id='DOw3L'>

              1. <legend id='DOw3L'><style id='DOw3L'><dir id='DOw3L'><q id='DOw3L'></q></dir></style></legend>

                  <bdo id='DOw3L'></bdo><ul id='DOw3L'></ul>
                    <tbody id='DOw3L'></tbody>
                  本文介绍了Base 64在Python中对JSON变量进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个存储json值的变量。我想用Python进行base64编码。但是抛出错误"不支持缓冲区接口"。我知道Base64需要一个字节来转换。但由于我是Python中的Newbee,不知道如何将json转换为base64编码的字符串。有没有直接的方法可以做到这一点??

                  推荐答案

                  在Python3.x中,您需要将str对象转换为bytes对象,以便base64能够对它们进行编码。您可以使用str.encode方法:

                  >>> import json
                  >>> import base64
                  >>> d = {"alg": "ES256"} 
                  >>> s = json.dumps(d)  # Turns your json dict into a str
                  >>> print(s)
                  {"alg": "ES256"}
                  >>> type(s)
                  <class 'str'>
                  >>> base64.b64encode(s)
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/usr/lib/python3.2/base64.py", line 56, in b64encode
                      raise TypeError("expected bytes, not %s" % s.__class__.__name__)
                  TypeError: expected bytes, not str
                  >>> base64.b64encode(s.encode('utf-8'))
                  b'eyJhbGciOiAiRVMyNTYifQ=='
                  

                  如果将your_str_object.encode('utf-8')的输出传递给base64模块,则应该能够对其进行正确编码。

                  这篇关于Base 64在Python中对JSON变量进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                    <legend id='d1JqX'><style id='d1JqX'><dir id='d1JqX'><q id='d1JqX'></q></dir></style></legend>
                      <i id='d1JqX'><tr id='d1JqX'><dt id='d1JqX'><q id='d1JqX'><span id='d1JqX'><b id='d1JqX'><form id='d1JqX'><ins id='d1JqX'></ins><ul id='d1JqX'></ul><sub id='d1JqX'></sub></form><legend id='d1JqX'></legend><bdo id='d1JqX'><pre id='d1JqX'><center id='d1JqX'></center></pre></bdo></b><th id='d1JqX'></th></span></q></dt></tr></i><div id='d1JqX'><tfoot id='d1JqX'></tfoot><dl id='d1JqX'><fieldset id='d1JqX'></fieldset></dl></div>
                          <tbody id='d1JqX'></tbody>
                          <tfoot id='d1JqX'></tfoot>
                          • <bdo id='d1JqX'></bdo><ul id='d1JqX'></ul>

                            <small id='d1JqX'></small><noframes id='d1JqX'>