python基础(6)--字符处理
2012-12-18 18:51
204 查看
[align=center] [/align]
[align=center][/align]
[align=left][/align]
1,字符串处理基本方法
.提取子字符串
[align=left]#法1:使用切片操作[/align]
>>> 'hello world'[2:8]
'llo wo'
[align=left][/align]
[align=left].合并两个字符串[/align]
[align=left]#法1:使用运算符+[/align]
>>> 'hello ' + 'world'
'hello world'
[align=left][/align]
[align=left]#法2: 使用运算符*[/align]
>>> 'hello '*2
'hello hello '
[align=left][/align]
[align=left]#使用join合并多个字符串[/align]
>>> '--'.join(['a','b','c'])
'a--b--c'
[align=left][/align]
[align=left].替换字符串[/align]
[align=left]#法1:使用replace函数[/align]
>>> 'hello world'.replace('world', 'tom')
'hello tom'
[align=left][/align]
[align=left].格式化字符串[/align]
[align=left]#法1: 使用格式化字符串[/align]
>>> 'hello %s %s' % ('world', 'haha')
'hello world haha'
[align=left][/align]
[align=left]#法2: 使用模板[/align]
>>> template = '--<html>--</html>'
>>> template = template.replace('<html>', 'start')
>>> template = template.replace('</html>', 'end')
>>> print template
--start--end
[align=left][/align]
[align=left]#使用模板2[/align]
>>> template = "hello %(key1)s"
>>> template = "hello %(key1)s %(key2)s"
>>> vals={'key1':'value1', 'key2':'value2'}
>>> print(template % vals)
hello value1 value2
[align=left][/align]
[align=left].分解字符串[/align]
>>> 'a b c d'.split() #基本分解
['a', 'b', 'c', 'd']
[align=left][/align]
[align=left]#指定分解符[/align]
>>> 'a+b+c+d'.split('+')
['a', 'b', 'c', 'd']
[align=left][/align]
[align=left]#分解和合并混用[/align]
from sys import *
stdout.write(('.' * 4).join(stdin.read().split('\t')))
[align=left]实战:[/align]
>>> stdout.write(('.' * 4).join(stdin.read().split('\t')))
aa bb cc dd
aa....bb....cc....dd
[align=left][/align]
[align=left][/align]
应用
#文本过滤
.基本方法
#条件程序
def isCond(astr):
'find sub string @fstr from a string @astr'
return (astr.find('root') != -1)
也可以用匿名函数:
isCond lambda astr: astr.find('root') != -1
#文本过滤第一版
def filter1(filename):
'filter every line which read from filename'
selected = []
try:
fp = open(filename)
except IOError, e:
print 'could not open file :', e
for line in fp.readlines():
if isCond(line):
selected.append(line)
print selected
#文本过滤第2版,使用filter内建函数
def filter2(filename):
'filter version 2'
selected = []
selected = filter(isCond, open(filename).readlines())
print selected
.使用map函数
map(isCond, open(filename).readline())
[align=center][/align]
[align=left][/align]
1,字符串处理基本方法
.提取子字符串
[align=left]#法1:使用切片操作[/align]
>>> 'hello world'[2:8]
'llo wo'
[align=left][/align]
[align=left].合并两个字符串[/align]
[align=left]#法1:使用运算符+[/align]
>>> 'hello ' + 'world'
'hello world'
[align=left][/align]
[align=left]#法2: 使用运算符*[/align]
>>> 'hello '*2
'hello hello '
[align=left][/align]
[align=left]#使用join合并多个字符串[/align]
>>> '--'.join(['a','b','c'])
'a--b--c'
[align=left][/align]
[align=left].替换字符串[/align]
[align=left]#法1:使用replace函数[/align]
>>> 'hello world'.replace('world', 'tom')
'hello tom'
[align=left][/align]
[align=left].格式化字符串[/align]
[align=left]#法1: 使用格式化字符串[/align]
>>> 'hello %s %s' % ('world', 'haha')
'hello world haha'
[align=left][/align]
[align=left]#法2: 使用模板[/align]
>>> template = '--<html>--</html>'
>>> template = template.replace('<html>', 'start')
>>> template = template.replace('</html>', 'end')
>>> print template
--start--end
[align=left][/align]
[align=left]#使用模板2[/align]
>>> template = "hello %(key1)s"
>>> template = "hello %(key1)s %(key2)s"
>>> vals={'key1':'value1', 'key2':'value2'}
>>> print(template % vals)
hello value1 value2
[align=left][/align]
[align=left].分解字符串[/align]
>>> 'a b c d'.split() #基本分解
['a', 'b', 'c', 'd']
[align=left][/align]
[align=left]#指定分解符[/align]
>>> 'a+b+c+d'.split('+')
['a', 'b', 'c', 'd']
[align=left][/align]
[align=left]#分解和合并混用[/align]
from sys import *
stdout.write(('.' * 4).join(stdin.read().split('\t')))
[align=left]实战:[/align]
>>> stdout.write(('.' * 4).join(stdin.read().split('\t')))
aa bb cc dd
aa....bb....cc....dd
[align=left][/align]
[align=left][/align]
应用
#文本过滤
.基本方法
#条件程序
def isCond(astr):
'find sub string @fstr from a string @astr'
return (astr.find('root') != -1)
也可以用匿名函数:
isCond lambda astr: astr.find('root') != -1
#文本过滤第一版
def filter1(filename):
'filter every line which read from filename'
selected = []
try:
fp = open(filename)
except IOError, e:
print 'could not open file :', e
for line in fp.readlines():
if isCond(line):
selected.append(line)
print selected
#文本过滤第2版,使用filter内建函数
def filter2(filename):
'filter version 2'
selected = []
selected = filter(isCond, open(filename).readlines())
print selected
.使用map函数
map(isCond, open(filename).readline())
相关文章推荐
- Python3.4字符串基础及每次处理一个字符
- JSON 基础 + PHP 处理 JSON + Python 处理 JSON
- python基础知识三——try与except处理异常语句
- python re 处理中文字符的问题
- python读取文件时遇到非法字符的处理 UnicodeDecodeError: 'gbk' codec can't decode bytes in position
- Python 异常处理 Python 基础教程 try..except
- Py修行路 python基础 (二十二)异常处理
- Python3基础 file read 读取txt文件的前几个字符
- Python 基础知识之字符串处理
- python基础---文件处理
- js,python正则表达式对特殊字符的处理
- Python美味食谱: 1.1 每次处理一个字符
- python 字符处理代码 1.0
- python基础学习(6)—使用Python处理文件
- python每次处理一个字符的三种方法
- Opencv3 python学习1——图片基础处理
- Node.js与PHP、Python的字符处理性能对比
- python基础--time,datatime日期处理
- python基础数据处理
- python处理csv文件名中中文字符解决方法