
Python
书山登峰人
本人从事软件开发约十年,熟悉JAVA、C++、Python、JavaScript等编程语言,专注于Web开发、AI开发、大数据开发,欢迎互相学习交流。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
001_015 Python 扩展和压缩制表符即将制表符替换为一定数目的空格或者相反
代码如下:#encoding=utf-8print '中国'#制表符转化为空格str='1\t2\n中\t国'ustr=u'中\t国'print str.expandtabs()print ustr.expandtabs(4)#空格转化为制表符 需求较少 处理单行字符串import re #正则表达式 将空格给位空格加tab 其中将'a'应该'\t','b'应该为' '原创 2014-03-10 23:52:19 · 1318 阅读 · 0 评论 -
001_016 Python 替换字符串中的子串
代码如下:#encoding=utf-8print '中国'#简单字符串替换str = 'abc'print str.replace('a', 'one')#根据字典内容,替换被标记的字符串def expand(format,d,marker='"',safe=False): #safe 如果字典中找不到,不会抛出异常 if safe:原创 2014-03-11 02:03:04 · 791 阅读 · 0 评论 -
001_014 Python 改变多行文本字符串的缩进
代码如下:#encoding=utf-8print '中国'#改变多行文本字符串的缩进def reindent(s,numSpaces): leading_space = numSpaces * ' ' lines = [leading_space + line.strip() for line in s.splitlines()] return '\n'.j原创 2014-03-10 22:50:36 · 2631 阅读 · 0 评论 -
001_018 Python 一次完成多个字符串替换
代码如下: #encoding=utf-8print '中国'# 一次完成多个字符串替换#利用正则表达式re的sub方法import redef multiple_replace(text,adict): rx = re.compile('|'.join(map(re.escape,adict))) def one_xlat(match): ret原创 2014-03-11 03:39:33 · 8322 阅读 · 0 评论 -
001_013 Python 字符串分割 访问子字符串
代码如下:#encoding=utf-8print '中国'#访问子字符串#方案一 切片 一次只能取一个print '------1'str ='abc中国'ustr =u'abc中国'print str[2:6] #相当于2-5 中文占了2个 因为是UTF-8print ustr[2:4] #unicode#方案二 struct.unpack 考虑长度prin原创 2014-03-10 20:23:32 · 692 阅读 · 0 评论 -
001_017 Python 替换字符串中的子串string.template
代码如下:#encoding=utf-8print '中国'#根据字典内容,替换被标记的字符串import stringnew_style=string.Template('this is $thing')print new_style.substitute({'thing':5})print new_style.substitute({'thing':'test some原创 2014-03-11 02:28:11 · 1010 阅读 · 0 评论 -
001_019 Python 检查字符串中的结束字符
代码如下:#encoding=utf-8print '中国'#检查字符串中的结束标记#方案一 endwith()print '-----------1'str ='abc\n\r\n'print str.endswith('\n')print str.endswith('\r\n')#方案二 使用itertools.imapimport itertools#ite原创 2014-03-12 01:25:50 · 2480 阅读 · 0 评论 -
001_020 Python 使用Unicode来处理国际化文本
代码如下:#encoding=utf-8print '中国'#使用Unicode来处理国际化文本 #方案 编码转换ANSI为Unicode后处理print 'abc 中国'print unicode('abc 中国')print type('abc')print type( unicode('abc 中国'))print type( 'abc'+ unicode('abc原创 2014-03-12 01:37:51 · 687 阅读 · 0 评论 -
001_024 Python 让某些字符串大小写不敏感 如比较和查询不敏感 其他敏感
代码如下:#encoding=utf-8print '中国'#让某些字符串大小写不敏感 如比较和查询不敏感 其他敏感#方案 封装为类class iStr(str): '''大小写不敏感的字符串类 类似str 比较和查询大小写不敏感 ''' def __init__(self,*args): self.原创 2014-03-12 19:37:48 · 5153 阅读 · 1 评论 -
001_022 Python 在标准输出上打印Unicode字符
代码如下:import sys,codecschar=u'\N{LATIN SMALL LETTER A WITH DIAERESIS}'sys.stdout=codecs.lookup('utf-8')[-1](sys.stdout)print char打印结果如下:ä原创 2014-03-12 02:03:37 · 912 阅读 · 0 评论 -
001_023 Python 对Unicode数据编码并应用于XML和HTML
代码如下:#encoding=utf-8print '中国'#对Unicode数据编码并应用于XML和HTML#解决方案def encode_for_xml(udata,encoding='ascii'): return udata.encode(encoding,'xmlcharrefreplace')print encode_for_xml(u'中国')打印结原创 2014-03-12 02:13:30 · 517 阅读 · 0 评论 -
001_021 Python 在Unicode和普通如ANSI编码之间转化
代码如下:#encoding=utf-8print '中国'#在Unicode和普通如ANSI编码之间转化ustr=u'abc'ustr2=u'abc中国'#一 unicode 转换为普通字符串print ustr.encode('ascii') #print ustr2.encode('ascii') # 这个失败 包含ascii表示不了的字符print ustr2原创 2014-03-12 01:49:35 · 8020 阅读 · 0 评论 -
001_002 Python 字符和字符值之间的转化
code如下:#coding=utf-8print '中国'print ord('a')print chr(97)print ord(u'a') #unicode和ansi值是一样的print ord(u'中')print unichr(20013) #unicodeprint repr(unichr(20013)) #规范表示print str(unichr(2原创 2014-03-08 19:10:52 · 1681 阅读 · 0 评论 -
001_004 Python 字符串对齐
代码如下:#encoding=utf-8print '中国'#字符串对齐print '|','abc中国'.ljust(20),'|','abc中国'.rjust(20),'|','abc中国'.center(20),'|'print u'|',u'abc中国'.ljust(20),u'|',u'abc中国'.rjust(20),u'|',u'abc中国'.center(20),u'原创 2014-03-08 21:49:01 · 763 阅读 · 0 评论 -
002_001 Python 读取文件
代码如下:#encoding=utf-8print '中国'#读取文件input = open(r'D:\123.txt','r')#方式一print '--------1'while True: line = input.readline() if not line : break else: print line.d原创 2014-03-13 01:18:10 · 950 阅读 · 0 评论 -
002_005 Python 计算一个文件中有多少行即读取文件行数
文件D:\123.txt的内容如下:1abc中国2abc中国3abc中国4abc中国5abc中国6abc中国读取文件行数代码如下:#encoding=utf-8print '中国'#计算一个文件中有多少行#文件比较小count = len(open(r"d:\123.txt",'rU').readlines())print count原创 2014-03-13 22:50:01 · 23794 阅读 · 0 评论 -
001_001 Python字符串一个个遍历
代码如下:#coding=utf-8print '中国'#每次处理一个字符串mystr = u'test string中国'#方案一 使用listliststr = list(mystr)for i in liststr: print '1-',i print liststr.__len__()print liststr[12]#方案二 使原创 2014-03-08 18:50:40 · 5522 阅读 · 0 评论 -
001_011 Python 检查一个字符串是文本还是二进制
代码如下:#encoding=utf-8print '中国'#检查一个字符串是文本还是二进制'''如果字符串中包含了控制或者其中有超过30%的字符高位为1那么就是二进制数据'''import stringtext = ''.join(map(chr,range(32,127))) + '\r\n\t\b'_null_trans = string.maketrans('','原创 2014-03-10 00:49:34 · 3495 阅读 · 0 评论 -
002_002 Python 写入文件
代码如下:#encoding=utf-8print '中国'#写入文件#方案一 注意 close才能保证数据写入file_object = open(r'd:\123.txt','w')file_object.write('123abc中国')file_object.close()file_object = open(r'd:\1234.txt','wb')file_o原创 2014-03-13 22:00:33 · 529 阅读 · 0 评论 -
002_003 Python 搜索和替换文件中的文本
代码如下:#encoding=utf-8print '中国'#搜索和替换文件中的文本srcText = '123'desText = '456'input_file = open(r'D:\123.txt')output_file = open(r'D:\1236.txt','w')for s in input_file: output_file.write(s.re原创 2014-03-13 22:21:46 · 610 阅读 · 0 评论 -
002_004 Python 从文件中读取某一行
D:\123.txt中内容如下:1abc中国2abc中国3abc中国4abc中国5abc中国6abc中国读取第五行代码如下:#encoding=utf-8print '中国'#从文件中读取某一行 linecache.checkcache可以刷新cache ,linecache可以缓存某一行的信息 import linecachethel原创 2014-03-13 22:39:59 · 11017 阅读 · 0 评论 -
002_008 Python 随机写入文件,更新随机存取的文件
代码如下:#encoding=utf-8print '中国'#固定长度的二进制文件,读取某些记录修改,然后写回去'''d:\123.txt的内容1 a b c 中 国2 a b c 中 国'''import structformat_string = '2c' #一个记录是2个字节record_size = struct.calcsize(format_string)r原创 2014-03-14 21:18:22 · 1955 阅读 · 0 评论 -
002_009 Python 从Zip中读取数据 直接检查一个zip格式的归档文件部分或所有文件而且不用解压
代码如下:#encoding=utf-8print '中国'#直接检查一个zip格式的归档文件部分或所有文件而且不用解压import zipfilezp = zipfile.ZipFile(r'D:\program files\ROMasterLab\Driver.zip','r')for filename in zp.namelist(): print 'File原创 2014-03-14 22:08:26 · 2895 阅读 · 0 评论 -
001_007 Python 将字符串逐字或者逐词翻转
代码如下:#encoding=utf-8print '中国'#将字符串逐字或者逐词翻转print '--1'#方案一 步长负一 实现翻转 最好的方法str = '123'print strprint str[::-1]print '--2'#方案二 序列 翻转 需要有分割符str = '1 2 3'liststr =原创 2014-03-09 02:58:20 · 1907 阅读 · 0 评论 -
001_009 Python 字符串Translate的使用
代码如下: 核心思想,keep = all - delete#encoding=utf-8print '中国'import string#字符串Translate的使用 不支持unicodedef translator(frm='',to='',delete='',keep=None): if len(to) == 1: #拓展 to = to * len(原创 2014-03-09 22:47:02 · 839 阅读 · 0 评论 -
001_005 Python 去除字符串两端的空格
代码如下:#encoding=utf-8print '中国'#去除字符串两端的空格str = ' abc 中国 'print '-'+str.lstrip()+'-'print '-'+str.rstrip()+'-'print '-'+str.strip()+'-'str = 'xx abc 中国 中国'print '-'+str.lstrip('x')+'-原创 2014-03-09 00:21:17 · 3962 阅读 · 0 评论 -
001_008 Python 检查字符串中是否包含某字符集合中的字符
代码如下: #encoding=utf-8print '中国'#检查字符串中是否包含某字符集合中的字符#方案 适用于字符串集合print '---1'def ContainAny(seq,aset): for c in seq: if c in aset: return True return Falseprint原创 2014-03-09 18:24:34 · 4866 阅读 · 1 评论 -
001_012 Python 字符串大小写转化
代码如下:#encoding=utf-8print '中国'#字符串大小写转化str ='aBc中国'ustr =u'aBc中国'print str.upper()print str.lower()print str.capitalize() #第一个大写其余小写print ustr.upper()print ustr.lower()print ustr.capital原创 2014-03-10 01:00:09 · 998 阅读 · 1 评论 -
002_017 Python 在目录树中改变文件扩展名 重命名一系列指定类型的文件
代码如下:#encoding=utf-8print '中国'#在目录树中改变文件扩展名 重命名一系列指定类型的 文件import osstr ='123'def swapextensions(dir,before,after): if before[:1] != '.': before = '.'+before thelen = -len(b原创 2014-03-16 19:52:51 · 727 阅读 · 0 评论 -
002_020 Python 在Python的搜索路径中寻找文件
代码如下:#encoding=utf-8print '中国'#在Python的搜索路径中寻找文件import sys,osclass Error(Exception):passdef _find(pathname, matchFunc=os.path.isfile): for dirname in sys.path: candidate = os.p原创 2014-03-16 21:54:40 · 560 阅读 · 0 评论 -
002_021 Python 动态改变Python的搜索路径
代码如下:#encoding=utf-8print '中国'#动态改变Python的搜索路径import sys,osdef AddSysPath(new_path): if not os.path.exists(new_path):return -1 new_path = os.path.abspath(new_path) if sys.platform原创 2014-03-17 01:41:02 · 811 阅读 · 0 评论 -
002_022 Python 计算目录间的相对路径
代码如下:#encoding=utf-8print '中国'#计算目录间的相对路径import os,itertoolsdef all_equal(elements): first_element = elements[0] for other_element in elements[1:]: if other_element != first_原创 2014-03-17 02:10:14 · 2327 阅读 · 0 评论 -
002_023 Python 跨平台读取无缓存字符
代码如下:#encoding=utf-8print '中国'#跨平台读取无缓存字符try: #windows import msvcrt except ImportError: #unix def getch(): import sys,tty,termios fd = sys.stdin.fileno() old_原创 2014-03-17 02:36:46 · 675 阅读 · 0 评论 -
002_024 Python 在Windows和Mac OS X平台上统计PDF的文档的页数
代码如下:#encoding=utf-8print '中国'#在Windows和Mac OS X平台上统计PDF的文档的页数#Windows#借助于PyPdf#1.下载http://pybrary.net/pyPdf/pyPdf-1.13.zip#2.注册Python环境变量path中假如D:\Python27#3.cmd中 cd D:\Python27原创 2014-03-17 03:09:11 · 2161 阅读 · 0 评论 -
001_006 Python 合并字符串
代码如下:#encoding=utf-8print '中国'#合并字符串#方案一 join合并数组类的一系列 因为不用像循环一样每次创建一个临时字符串,所以性能比较高liststr = ['1','2','3']print ''.join(liststr)print '-'.join(liststr)largestr=''for str in liststr: l原创 2014-03-09 01:47:31 · 766 阅读 · 0 评论 -
001_010 Python 过滤字符串中不属于指定集合的元素
代码如下:#encoding=utf-8print '中国'#输出字符串中属于某一个集合的字符 not support unicodeprint '不支持unicode'import stringallchars = string.maketrans('','')def makefilter(keep): delchars = allchars.translate(原创 2014-03-09 23:48:23 · 1390 阅读 · 0 评论 -
002_011 Python 将整个目录压缩为Tar文件,并指定压缩方式如gz,bz2
代码如下: #encoding=utf-8print '中国'#将文件树(目录树)归档到一个压缩文件tarimport tarfile,osdef mak_tar(foldername, dest_folder, compression='bz2'): if compression: dest_ext = '.' + compression el原创 2014-03-15 14:30:56 · 2611 阅读 · 0 评论 -
002_012 Python 将二进制数据发送到windows标准输出,即打印二进制到控制台
代码如下:#encoding=utf-8print '中国'#将二进制数据发送到windows标准输出#如图片打印到std.outimport sysprint sys.platformif sys.platform =='win32': import os,msvcrt msvcrt.setmode(sys.stdout.fileno(),os.O_BI原创 2014-03-15 14:41:57 · 1889 阅读 · 0 评论 -
002_013 Python 使用C++类的iostream语法
代码如下:#encoding=utf-8print '中国'#使用C++类的iostream语法#如果要使用<<需要定义__lshift__#关联IO函数class IOManipulator(object): def __init__(self,function=None): self.function = function def do(s原创 2014-03-15 15:14:09 · 1377 阅读 · 0 评论 -
002_015 Python 用类对象适配真实文件对象
代码如下:#encoding=utf-8print '中国'#用类对象适配真实文件对象#传递一个类似文件的对象 如 urllib.urlopen给一个函数或者方法,但这个函数只接受真实的文件对象import types,tempfile#解决方案,吧类对象写入一个临时文件,然后再传入。CHUNK_SIZE= 16 * 1024import os, sysdef ada原创 2014-03-16 17:33:51 · 552 阅读 · 0 评论