字符和文本
- 使用多个界定符分割字符串
import re
line = 'asdf fjdk; afed, fjek,asdf, foo'
l = re.split(r"[;,\s]\s*", line)
print(l)
分组匹配分割符也会在数组中
l2 = re.split(r"(;|,|\s)\s*", line)
print(l2)
print(".".join(l))
不想匹配可加?:,非捕获
l3 = re.split(r"(?:;|,|\s)\s*", line)
print(l3)
输出:['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
['asdf', ' ', 'fjdk', ';', 'afed', ',', 'fjek', ',', 'asdf', ',', 'foo']
asdf.fjdk.afed.fjek.asdf.foo
- 用 Shell 通配符匹配字符串
from fnmatch import fnmatch, fnmatchcase
l = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']
lr = [n for n in l if fnmatch(n, "Da*.csv")]
print(lr)
s = 'dir.TXT'
print(fnmatch(s, "*.txt"))
# 忽略大小写
print(fnmatchcase(s, "*.txt"))
addresses = [
'5412 N CLARK ST',
'1060 W ADDISON ST',
'1039 W GRANVILLE AVE',
'2122 N CLARK ST',
'4802 N BROADWAY',
]
print([n for n in addresses if fnmatch(n,"* st")])
find可以返回字的索引,-1表示无返回值
txt = "我也是醉了"
tr = txt.find("是")
print(tr)
- 字符串替换 re.sub,replace,isupper,islower,
re最短匹配模式 .*?
- re.IGNORECASE 能忽略大小写
import re
text = 'UPPER PYTHON, lower python, Mixed Python'
tr = re.findall('py.hon', text, re.I)
print(tr)
text = '''
你是一只猪么
么么
'''
import re
rt = re.findall('么\n么么', text)
print(rt)
- strip去掉首尾字符,replace好东西,可以多用几次
str1 = "--------1==//,,,=b===..,,..."
s = str1.strip("-.,=/")
print(s)
def clean_spaces(s):
s = s.replace('\r', '')
s = s.replace('\t', ' ')
s = s.replace('\f', ' ')
return s
- 对于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center(),format()
方法。
text="helloword"
print(text.center(20,"="))
text = "helloword"
print(text.center(20, "="))
f1 = format("111", ">20")
print(f1)
f2 = format("111", "(>20")
print(f2)
f3 = format("aaa", "+^20")
print(f3)
s1 = "{:=^20s}".format("1111")
print(s1)
- 字符串拼接,分割textwrap
def sample():
yield 'Is'
yield 'Chicago'
yield 'Not'
yield 'Chicago?'
f = ",".join(sample())
print(f)
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."
import textwrap
tf = textwrap.fill(s, 40)
print(tf)
- 字节比字符速度快