可爱的python课后习题(三)

1,判定某个blog的编码方式:

#!/usr/bin/python
#coding=utf-8
#filename:codingTest.py
import sys
import urllib2
import chardet

def blog_detect(blogurl):
        try:
                fp=urllib2.urlopen(blogurl)
        except Exception,e:
                print e
                print 'download exception %s' %blogurl
                return 0
        blog=fp.read()
        print blog
        codedetect=chardet.detect(blog)["encoding"]
        print '%s------>%s' %(blogurl,codedetect)
        fp.close()
        return 1
if __name__=='__main__':
        if len(sys.argv)==1:
                print 'usage:\n\t python codingTest.py http://'
        else:
                blog_detect(sys.argv[1])
测试结果:

root@zhou:/home/zhouqian/python# python codingTest.py http://www.baidu.com
http://www.baidu.com------>GB2312
root@zhou:/home/zhouqian/python# python codingTest.py http://www.google.cn
http://www.google.cn------>utf-8
遇到的问题总结:

打开url链接所用到的urllib2这个模块,第一次接触学习下

还有chardet用来检测字符类型的模块,使用方法chardet.detect(blog)["encoding"]返回字符类型,上面已经测试了

然后就是文本的编码方式,以及转换方法。其中对于python2来说默认的编码方式ASCII,对于python3来说默认的编码方式:unicode。

他们存储文本信息时大致的过程是先解码转换成对应的二进制格式,输出时再通过相应编码格式,呈现出来。

习题2.不是utf-8的变成utf-8格式的

#!/usr/bin/python
#coding=utf-8
#filename:codingTest.py
import sys
import urllib2
import chardet

def blog_detect(blogurl):
        try:
                fp=urllib2.urlopen(blogurl)#了解下就可以了。
        except Exception,e:
                print e
                print 'download exception %s' %blogurl
                return 0
        blog=fp.read()
        print blog
        codedetect=chardet.detect(blog)["encoding"]#主要的操作之一
        print '%s------>%s' %(blogurl,codedetect)
        fp.close()
        print '########进行转换#####'
        if codedetect<>'utf-8':
                try:
                        #这里是代码的核心,也是python文本编码的主要用法所在unicode和ASCII这两种编码方式
                        blog=unicode(blog,codedetect)#进行解码操作
                        blog=blog.encode('utf-8')#进行编码操作
                except:
                        print 'bad unicode encode try'
                        print 'failed convert'
                        return 0
        filename='%s_utf-8' %blogurl[7:]#存储的是文件名以博客的链接来命名的
        filename=filename.replace('/','_')
        open(filename,'w').write(blog)
        print 'save to file %s' %filename
        return 1
if __name__=='__main__':
        if len(sys.argv)==1:
                print 'usage:\n\t python codingTest.py http://'
        else:
                blog_detect(sys.argv[1])
测试结果:

zhouqian@zhou:~/python$ python codingConvert.py http://www.baidu.com
http://www.baidu.com------>GB2312
########进行转换#####
save to file www.baidu.com_utf-8
生成这么一个文件,可以看到中文字符,之前没有转换的时候是乱码。

遇到的问题跟上面差不多,两者结合而已。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值