随笔分类 -  Python

摘要:乱码产生的原因是Python在读取时默认解码方式是用操作系统编码,如果和保存时的编码方式不一样,就会出现乱码比如以下片段,文件保存格式是utf-8#coding=utf-8print '是' #输出乱码因为windows默认的编码方式是GBK,python文件保存时使用了utf-8,在读取时,python使用GBK的编码表去解utf-8编码的字节码,因为GBK与UTF-8编码不兼容,自然出现了乱码问题解决方法:1.直接使用 u'是' 形式,指明以unicode编码,解码方式会以顶部 #coding定义的编码方式,如果不写,以操作系统当前编码方法,建议写上#cod 阅读全文
posted @ 2013-04-06 20:13 Bug山Bug海 阅读(18231) 评论(0) 推荐(0)
摘要:内置函数map(function, iterable, ...)讲传入迭代器的值映射为另一个新值的列表map还可以接受多个iterable作为参数,在第n次调用function时,将使用iterable1[n], iterable2[n], ...作为参数。filter(function, iterable)这个函数的功能是过滤出iterable中所有以元素自身作为参数调用function时返回True或bool(返回值)为True的元素并以列表返回,与系列第一篇中的my_filter函数相同。zip(iterable1, iterable2, ...)这个函数返回一个列表,每个元素都是一个元 阅读全文
posted @ 2012-12-16 16:01 Bug山Bug海 阅读(175) 评论(0) 推荐(0)
摘要:#!-*-coding: UTF-8 -*-import redef repfuc(match): return "["+match.group(1)+"]"s="111AA22BB33"rnum="(?P\d+)"print(re.match(rnum,s).group(1)) #获取第一个匹配,第一个是整个匹配print(re.match(rnum,s).groups()[0])#获取匹配列表,没有全匹配,和上一个一样print("re.sub")print(re.sub(rnum,&quo 阅读全文
posted @ 2012-12-06 18:41 Bug山Bug海 阅读(354) 评论(0) 推荐(0)
摘要:open(filepath,mode)打开文件,返回file object mode:a追加,w覆写,不存在文件创建,r读取os.path.join(str[]) 平台相关合并路径,返回合并后路径字符串os.path.split(str) 返回2个项的元组,父路径以及最后一个路径组件分割所有路径函数 : def split_full(path): parent_path,name=os.path.split(path) if name=="": return (parent_path,) else: return split_full(paren... 阅读全文
posted @ 2012-12-06 14:47 Bug山Bug海 阅读(176) 评论(0) 推荐(0)
摘要:import os,sysimport redef walkdir(dirname,dep=1): if(dep<=0): return try: ls=os.listdir(dirname) except: print ('access deny '+dirname) else: for l in ls: temp=os.path.join(dirname,l) if(os.path.isdir(temp)): walkdi... 阅读全文
posted @ 2012-12-04 17:40 Bug山Bug海 阅读(206) 评论(0) 推荐(0)