跟我学python(4)- 正则
1. 引入模块
import re
2. 使用foreach循环
for iterating_var in sequence:
statements(s)
1) 名称解释
-(1)list(列表)
可以理解为java的数组和列表的综合体。
3. 使用模块
1)sub方法
替换符合正则要求的字符串。
sub(pattern, repl, string, count=0, flags=0)
4. code
hellopython.py
'''
Created on Dec 12, 2018
@author: xiaobin
'''
import os
import re
#! /usr/bin/perl
# @lines=`perldoc -u -f sin`;
# foreach(@lines){
# s/\w<([^>]+)/\U$1/g;
# print;
# }
cmdPydoc = 'pydoc math.sin'
lines = os.popen(cmdPydoc, 'r', -1).readlines()
for echostr in lines :
str1 = re.sub('([.]+){3}', "num", echostr, re.S)
print(str1)
else:
print("---- over ----")
#### 1)关于os.system和os.popen
相同点:都是调用shell。
不同点:
- os.system只关心结果;
- os.popen更关心过程。
5. 效果图 - WSL1.0
install pydoc
sudo apt install python-dev-is-python3

浙公网安备 33010602011771号