Base64系列第二篇 python中使用Base64编码解码

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

本文地址:http://blog.csdn.net/morewindows/article/details/11922473转载请标明出处,谢谢。

欢迎关注微博:http://weibo.com/MoreWindows  

 

本系列一共四篇:

1. 《Base64系列第一篇 Base64介绍

2. 《Base64系列第二篇 python中使用Base64编码解码

3. 《Base64系列第三篇 C/C++中使用Base64编码解码(使用boost库)

4. 《Base64系列第四篇 C/C++中使用Base64编码解码(从chromium库中抽取)

本篇《Base64系列第二篇 python中使用Base64编码解码》将介绍如何使用python来完成Base64的编码解码

 

在python中使用base64编码和解码都是非常方便的,在import base64模块后直接使用encodestring()和decodestring()就可以了,先使用基于URL的改进Base64编码的同样方便,python已经提供了urlsafe_b64encode()urlsafe_b64decode()供使用。

下面给出python的示范代码:

# http://blog.csdn.net/morewindows/article/details/11922473# By MoreWindows ( http://blog.csdn.net/MoreWindows )# more info please visit http://www.python.org/doc//current/library/base64.htmlimport base64text = "MoreWindows - http://blog.csdn.net/morewindows?viewmode=contents ~!@#$%"# encodestring(string) and decodestring(string)print "------------------------------------"print "origin text: "print textbase64_text = base64.encodestring(text)print "encode: "print base64_textprint "decode: "print base64.decodestring(base64_text)print "------------------------------------"# urlsafe_b64encode(string) and urlsafe_b64decode(string)print "------------------------------------"print "origin text: "print texturlsafe_base64_text = base64.urlsafe_b64encode(text)print "url safe encode: "print urlsafe_base64_textprint "url safe decode: "print base64.urlsafe_b64decode(urlsafe_base64_text)print "------------------------------------"

运行结果如下:

              

要对文件操作该怎么办了,python也提供了相应的接口,示范代码如下:

# http://blog.csdn.net/morewindows/article/details/11922473# By MoreWindows ( http://blog.csdn.net/MoreWindows )# more info please visit http://www.python.org/doc//current/library/base64.html# base64.encode(file1, file2)f1 = open('aaa.txt', 'r')f2 = open('bbb.txt', 'w')base64.encode(f1, f2)f1.close()2.close()

下面二篇《Base64系列第三篇 C/C++中使用Base64编码解码(使用boost库)》和《Base64系列第四篇 C/C++中使用Base64编码解码(从chromium库中抽取)》将介绍在C/C++中使用Base64编码和解码,欢迎继续浏览。


 

本文地址:http://blog.csdn.net/morewindows/article/details/11922473转载请标明出处,谢谢。

欢迎关注微博:http://weibo.com/MoreWindows  

 

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值