Python quopri模块,pythonquopri模块,quopri模块和bas


quopri模块和base64模块有点象,都是用来编码和解码的,且base64和quoted-printable这两种编码都是在电子邮件中常见的编码。

quoted-printable的编码方法为: 英文字符除了=以外不做处理,其他字符的编码为=加这个字符的两个字节的16进制数。行尾可用"=\r\n"。

quopri模块只需要使用它的encode,decode,encodestring,decodestring就可以了,前面两个是对文件进行编解码的(也可以对StringIO中的数据编码解码),后面两个是对字符串进行编码解码的。

#-*- encoding: gb2312 -*-import quopria = "only a test数据"b = quopri.encodestring(a) # 对字符串编码print bprint quopri.decodestring(b) # 对字符串解码import StringIOc = StringIO.StringIO()d = StringIO.StringIO()e = StringIO.StringIO()c.write(a)c.seek(0)quopri.encode(c, d, 0)  # 编码StringIO中的数据, 第三个参数0表示不对空格和tab符号编码,为1表示进行编码print d.getvalue()d.seek(0)quopri.decode(d, e)  # 解码StringIO中的数据print e.getvalue()f1 = open("aaa.txt", "w")f1.write(a)f1.close()f1 = open("aaa.txt", "r")f2 = open("bbb.txt", "w")quopri.encode(f1, f2, 0) # 编码aaa.txt中的数据到bbb.txtf1.close()f2.close()print open("bbb.txt", "r").read()#该片段来自于http://byrx.net

评论关闭