可爱的python课后习题(三)
2012-07-04 11:34
344 查看
1,判定某个blog的编码方式:
打开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() 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生成这么一个文件,可以看到中文字符,之前没有转换的时候是乱码。
遇到的问题跟上面差不多,两者结合而已。
相关文章推荐
- 可爱的python课后习题(二)
- 可爱的python课后习题
- Python 从入门到实践 7-4 课后习题
- 小甲鱼python视频第十讲(课后习题)
- Python课本第六章课后习题选做
- 【鱼C·零基础入门学习Python】课后习题答案 第14课
- Python 从入门到实践 7-8 课后习题
- Python学习笔记: 第五章课后习题练习
- Python课本第十章课后习题选做
- Python 从入门到实践 8-9 课后习题
- Python核心编程 第七章课后习题
- 小甲鱼python视频第四讲(课后习题)
- 中国大学 MOOC 课程 《Python 语言程序设计》第六周课后习题
- 小甲鱼python视频第九讲(课后习题)
- Python课本第九章课后习题选做
- 笨方法学Python 习题 39: 字典, 可爱的字典
- Python课本第三章课后习题选做
- 小甲鱼python视频第五讲(课后习题)
- Python 从入门到实践 8-6 课后习题
- 可爱的python习题(四)