python中encode()编码字符串的用法 |
您所在的位置:网站首页 › python中哈希用法 › python中encode()编码字符串的用法 |
encode()方法描述
encode()方法为python中内置的字符串方法,可以用encode()方法中的参数指定的编码格式来对调用的字符串对象进行编码。在机器学习当中,因为要经常读取保存在文件中的数据,因此经常会遇到文件读取的编码格式的设置。 encode()方法参数及用法str.encode( encoding = 'utf-8', errors='strict') 1、encoding参数指定encode()编码字符串过程中所要使用的编码格式,如utf-8。2、第二个参数errors指定python中Error的处理方法,默认情况下为strict,即抛出一个UnicodeError。除此之外,errors可能的值还包括:'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 等,以及通过 codecs.register_error() 注册其它值。 encode()方法返回值encode()方法返回经指定格式编码后的字符串,是一个新的字符串,因为encode()并为修改原字符串的值; encode()方法实例代码 >>> str = '笨鸟工具,x1y1z1.com' >>> str.encode('gbk') b'\xb1\xbf\xc4\xf1\xb9\xa4\xbe\xdf\xa3\xacx1y1z1.com' >>> str '笨鸟工具,x1y1z1.com' #变量str的值并没有变化 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |