Python encode()方法
Python encode()方法
Python encode() 方法以 encoding 指定的編碼格式編碼字符串。errors參數(shù)可以指定不同的錯(cuò)誤處理方案。
語(yǔ)法
encode()方法語(yǔ)法:
str.encode(encoding='UTF-8',errors='strict')
參數(shù)
- encoding -- 要使用的編碼,如"UTF-8"。
- errors -- 設(shè)置不同錯(cuò)誤的處理方案。默認(rèn)為 'strict',意為編碼錯(cuò)誤引起一個(gè)UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通過(guò) codecs.register_error() 注冊(cè)的任何值。
返回值
該方法返回編碼后的字符串。
實(shí)例
以下實(shí)例展示了encode()方法的實(shí)例:
#!/usr/bin/python str = "this is string example....wow/b64/"; print "Encoded String: " + str.encode('base64','strict')
以上實(shí)例輸出結(jié)果如下:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
相關(guān)文章
- Python 教程
- Python 中文編碼
- Python 變量類型
- Python 運(yùn)算符
- Python 列表 List
- Python 模塊
- Python CGI編程
- Python 搜索算法
- Python 大O符號(hào)
- Python 算法類
- Python3 for while 循環(huán)語(yǔ)句
- Python3 OS 文件/目錄方法
- Python3 多線程
- Python os.close() 方法
- Python os.lseek() 方法
- Python os.mkfifo() 方法
- Python os.ttyname() 方法
- Python splitlines()方法
- Python 字典 Dictionary pop() 方法
- Python time strptime()方法