In [1]:
# 多行结果输出支持
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
In [2]:
line = 'asdf fjdk; afed, fjek,asdf, foo'
import re
re.split(r'[;,\s]\s*', line)
Out[2]:
In [9]:
filename = 'spam.txt'
filename.endswith('.txt')
filename.startswith('file:')
url = 'http://www.python.org'
url.startswith('http:')
Out[9]:
Out[9]:
Out[9]:
In [10]:
filenames = [ 'Makefile', 'foo.c', 'bar.py', 'spam.c', 'spam.h' ]
[name.endswith('.py') for name in filenames]
# 是否存在 True
any(name.endswith('.py') for name in filenames)
Out[10]:
Out[10]:
In [12]:
from fnmatch import fnmatch, fnmatchcase
fnmatch('foo.txt', '*.txt')
Out[12]:
In [13]:
fnmatch('foo.txt', '?oo.txt')
fnmatch('Dat45.csv', 'Dat[0-9]*')
Out[13]:
Out[13]:
In [14]:
names = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']
[name for name in names if fnmatch(name, 'Dat*.csv')]
Out[14]:
In [18]:
text = 'yeah, but no, but yeah, but no, but yeah'
text
# 所有都会替换
text.replace('yeah', 'yep')
# 只替换前2个
text.replace('yeah', 'yep', 2)
Out[18]:
Out[18]:
Out[18]:
In [20]:
text = 'UPPER PYTHON, lower python, Mixed Python'
# 查找
re.findall('python', text, flags=re.IGNORECASE)
# 替换
re.sub('python', 'snake', text, flags=re.IGNORECASE)
Out[20]:
Out[20]:
In [25]:
# Whitespace stripping
s = ' hello world \n'
s
s.strip()
Out[25]:
Out[25]:
In [27]:
# 从左边
s.lstrip()
s.rstrip()
Out[27]:
Out[27]:
In [29]:
# Character stripping
t = '-----hello====='
# 去掉左边的 '-'
t.lstrip('-')
# 去掉 '-='
t.strip('-=')
Out[29]:
Out[29]:
In [34]:
text = 'Hello World'
text.ljust(20)
text.rjust(20)
text.center(20)
text.ljust(20, '*')
text.center(20, '*')
Out[34]:
Out[34]:
Out[34]:
Out[34]:
Out[34]:
In [39]:
format(text, '>20')
format(text, '<20')
format(text, '^20')
format(text, '=>20s')
Out[39]:
Out[39]:
Out[39]:
Out[39]:
In [40]:
'{:>10s} {:>10s}'.format('Hello', 'World')
Out[40]:
In [50]:
str1 = 'hello'
str2 = 'world'
# 生成一个元组
*str1, *str2
(*str1, *str2)
# list
[*str1, *str2]
# set
{*str1, *str2}
#dict
{key: value for key, value in zip(str1, str2)}
Out[50]:
Out[50]:
Out[50]:
Out[50]:
Out[50]:
In [52]:
str1.join(str2)
Out[52]:
In [53]:
str1 + str2
Out[53]:
In [54]:
parts = ['Is', 'Chicago', 'Not', 'Chicago?']
' '.join(parts)
Out[54]:
In [57]:
x = [1, 2, 4, -1, 0, 2]
def sam(arg):
yield(arg)
for o in sam(x):
o
Out[57]:
In [58]:
s = '{name} has {n} messages.'
s.format(name='Guido', n=37)
Out[58]:
In [59]:
name = 'Guido'
n = 37
s.format_map(vars())
Out[59]:
In [62]:
# 同 Linux 一样, python 中同样可以使用 \ 表示换行
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."
In [63]:
s
Out[63]:
In [65]:
data = b'Hello World'
data
data[0:5]
data.startswith(b'Hello')
data.split()
data.replace(b'Hello', b'Hello Cruel')
Out[65]:
Out[65]:
Out[65]:
Out[65]:
Out[65]:
In [67]:
data = bytearray(b'Hello World')
data
data[0:5]
data.startswith(b'Hello')
data.split()
data.replace(b'Hello', b'Hello Cruel')
Out[67]:
Out[67]:
Out[67]:
Out[67]:
Out[67]:
In [ ]: