xiaobin_hlj80

https://blog.csdn.net/xiaobin_HLJ80 http://blog.chinaunix.net/uid/31552151.html

导航

跟我学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

command-prompt_out3

Reference:

  1. Python for Loop Statements
  2. Python - Lists
  3. pydoc的用法

posted on 2026-02-03 15:51  xiaobin80  阅读(8)  评论(0)    收藏  举报