Python--进阶处理2

# ===================第二章:字符串和文本======================

# -----------------使用多个界定符分割字符串--------------------
# 需要更加灵活的切割字符串的时候,最好使用re.split() 方法
import re
line = 'asdf fjdk; afed, fjek,asdf, foo'
line_list = re.split(r'[;,\s]\s*', line)
print(line_list)

# -----------------字符串开头或结尾匹配---------------------
# 检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str.endswith() 方法
# 如果想检查多种匹配可能,只需要将所有的匹配项放入到一个元组中去,然后传给startswith() 或者endswith()
# 类似的操作也可以使用切片来实现
list_file = ['a.py', 'b.txt', 'c.pyc', 'd.java']
file_tuple = ('.py', '.java')
file_list = [name for name in list_file if name.endswith(file_tuple)]
print(file_list)

# ------------------用shell通配符匹配字符串-------------------
# fnmatch 模块提供了两个函数—— fnmatch() 和fnmatchcase() ,可以用来实现这样的匹配
from fnmatch import fnmatch, fnmatchcase
flag1 = fnmatch('file.txt', '*.txt')
print(flag1)
flag2 = fnmatch('Dat45.csv', 'Dat[0-9][0-9].c*')
print(flag2)
# fnmatch() 函数使用底层操作系统的大小写敏感规则(不同的系统是不一样的) 来匹配模式
# 如果你对这个区别很在意,可以使用fnmatchcase() 来代替
# fnmatch() 函数匹配能力介于简单的字符串方法和强大的正则表达式之间

# ------------------字符串匹配和搜索---------------------------
# 如果想匹配的是字面字符串,那么通常只需要调用基本字符串方法就行,比如str.find() , str.endswith() , str.startswith() 或者类似的方法
# 对于复杂的匹配需要使用正则表达式和re 模块
import re
# 将模式字符串预编译为模式对象
datepat = re.compile(r'\d+/\d+/\d+')
text1 = '11/27/2012'
text2 = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
# match() 总是从字符串开始去匹配,如果想查找字符串任意部分的模式出现位置,使用findall() 方法去代替
result1 = re.match(datepat, text1).group()
print(result1)
result2 = re.findall(datepat, text2)
print(result2)
# 在定义正则式的时候,通常会利用括号去捕获分组
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
m = datepat.match(text1)
print(m.group(0))
print(m.groups())
print(m.group(1))
print(m.group(2))
print(m.group(3))
# 如果想以迭代方式返回匹配,可以使用finditer() 方法来代替

# -------------字符串搜索和替换-------------------
# 对于简单的字面模式,直接使用str.repalce() 方法即可
text= 'yeah, but no, but yeah, but no, but yeah'
text_replace = text.replace('yeah', 'hello world')
print(text_replace)
# 对于复杂的模式,请使用re 模块中的sub() 函数
import re
text_1 = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
text_2 = datepat.sub(r'\3-\1-\2', text_1)
print(text_2)
print(re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text_1))

# --------------字符串忽略大小写的搜索替换-----------------
# 为了在文本操作时忽略大小写,需要在使用re 模块的时候给这些操作提供re.IGNORECASE 标志参数
text = 'UPPER PYTHON, lower python, Mixed Python'
list_1 = re.findall('python', text, flags=re.IGNORECASE)
print(list_1)
print(re.sub('python', 'Hello world', text, flags=re.IGNORECASE))

# --------------删除字符串中不需要的字符---------------------
# strip() 方法能用于删除开始或结尾的字符
s = ' hello world \n'
print(s.strip())
# 如果想处理中间的空格,那么需要求助其他技术。比如使用replace() 方法或者是用正则表达式替换

# ----------------------字符串对齐----------------------------
# 对于基本的字符串对齐操作,可以使用字符串的ljust() , rjust() 和center()方法
text = 'Hello World!'
print(text.ljust(20, '='))
# 函数format() 同样可以用来很容易的对齐字符串。你要做的就是使用<,> 或者ˆ 字符后面紧跟一个指定的宽度
print(format(text, '*^20s'))

# 合并拼接字符串
# 如果合并的字符串是在一个序列或者iterable 中,那么最快的方式就是使用join() 方法
parts = ['Is', 'Chicago', 'Not', 'Chicago?']
parts_str = ' '.join(parts)
print(parts_str)
# 如果仅仅只是合并少数几个字符串,使用加号(+) 通常已经足够了
# 需要引起注意的是,当我们使用加号(+) 操作符去连接大量的字符串的时候是非常低效率的,因为加号连接会引起内存复制以及垃圾回收操作

# ----------------------字符串中插入变量-----------------------------
s = '{name} has {n} message.'
print(s.format(name='qf', n=18))

# ----------------------以指定列宽格式化字符串-----------------------
# 使用textwrap 模块来格式化字符串的输出
import textwrap
s = "Look into my eyes, look into my eyes, the eyes, the eyes, \
the eyes, not around the eyes, don't look around the eyes, \
look into my eyes, you're under."
print(textwrap.fill(s, 70))



posted on 2018-02-06 16:18  我要的明天  阅读(246)  评论(0编辑  收藏  举报

导航