教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 Python encode()方法

Python encode()方法

发布时间:2021-12-11   编辑:jiaochengji.com
教程集为您提供Python encode()方法等资源,欢迎您收藏本站,我们将为您提供最新的Python encode()方法资源

描述

Python encode() 方法以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。

语法

encode()方法语法:

<pre class="brush:html;toolbar:false">string.encode(encoding='UTF-8',errors='strict')</pre>

参数

encoding -- 要使用的编码,如"UTF-8"。

errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。

返回值

该方法返回编码后的字符串。

实例

以下实例展示了encode()方法的实例:

<pre class="brush:html;toolbar:false">#!/usr/bin/python   string = "this is string example....wow!!!";   print "Encoded String: "   string.encode('base64','strict')</pre>

以上实例输出结果如下:

<pre class="brush:html;toolbar:false">Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=</pre>

<span style="color: rgb(0, 0, 0); font-family: Verdana, sans-serif; font-size: 15px; white-space: normal; background-color: rgb(255, 255, 255);">
</span>

您可能感兴趣的文章:
中文在python中占几个字节
python command乱码怎么解决
Python中的gbk怎么转化成utf
Python encode()方法
python中encoding是什么意思
什么是Python的字符串
PHP DES加解密方法代码
sublime编译python脚本出错了怎么办
python log中文乱码
Python常用模块之hashlib

[关闭]
~ ~