re.findall 和 re.sub的用法

1、re.findall()

样例:

picPattern = r"<img.*?src=[\"|\'].*?[\"|\'][^>]*?>"  # 匹配文章中的图片
def getPics(str):
    pics = re.compile(picPattern, re.I).findall(str)   #正则表达式中忽略大小写
    return pics

2、re.sub()

re.sub的参数:有五个参数

re.sub(pattern, repl, string, count=0, flags=0)

其中三个必选参数:pattern, repl, string

两个可选参数:count, flags

(1)pattern:该参数表示正则中的模式字符串;
(2)repl:该参数表示要替换的字符串(即匹配到pattern后替换为repl),也可以是个函数;
(3)string:该参数表示要被处理(查找替换)的原始字符串;
(4)count:可选参数,表示是要替换的最大次数,而且必须是非负整数,该参数默认为0,即所有的匹配都会被替换;
(5)flags:可选参数,表示编译时用的匹配模式(如忽略大小写、多行模式等),数字形式,默认为0。

样例:

def replacePics(str):
    str = re.sub(picPattern, 'my name', str, flags=re.I)
    return str

完整样例:

# coding:utf-8
import re
import json
picPattern = r"<img.*?src=[\"|\'].*?[\"|\'][^>]*?>"  # 匹配文章中的图片

str = '''
<div id="content">
                            <h2>思维导图</h2>
<p>思维导图:思维导图又叫心智导图是表达发散性思维的有效的图形思维工具,它简单却又很有效,是一种革命性的思维工具。思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出来,把主题关键词与图像、颜色等建立记忆链接。思维导图充分运用左右脑的机能,利用记忆、阅读、思维的规律,协助人们在科学与艺术、逻辑与想象之间平衡发展,从而开启人类大脑的无限潜能。思维导图因此具有人类思维的强大功能。</p>
<p>思维导图是一种将思维形象化的方法。简单来讲:思维导图就是能有层次感的展示我们想法的思维工具。</p>
<h2>思维导图功能</h2>
<p>无论是在工作还是生活中,我们都希望达成一个关键目标——提高学习、记忆和记录信息的能力。思维导图就是让人的大脑对知识有一个层次性的放射性记忆,从而得到一张清晰准确的图形。让想法可以很快而且非常深刻地生发出来,同时又能清晰地集中于中心主题。这种制作技巧能让人们最大限度地利用自己潜在的智力资源。</p>
<p>简单来讲,思维导图可以帮助我们思考问题解决问题,促进思维可视化,最大限度的开发我们的大脑潜能。</p>
<h2>使用软件</h2>
<p>画思维导图的工具有很多,这里小编推荐使用 Xmind 工具,下载官网下载后直接安装即可。</p>
<p>安装好以后进入 Xmind 工具操作界面,界面如下:</p>
<p style="text-align: center"><IMG alt="图片.png" src="//img.jbzj.com/file_images/article/202110/2021102816381395.png" style="width: 800px; height: auto;"></p>
<p>Xmind 操作很简单,只要思路对了,只需要常用的快捷键就可以快速操作,以下是 Xmind 操作的相关快捷键。</p>
<p style="text-align: center"><img alt="图片.png" src="//img.jbzj.com/file_images/article/202110/2021102816381396.png" style="width: 800px; height: auto;"></p>
<p style="background-color: rgb(231, 234, 241); text-align: center; padding: 0px; margin: 20px 0px;"><img style="border:none;padding:0" src="https://img.jbzj.com/file_images/article/202104/2021416141955635.png" alt="Python客栈送红包、纸质书"></p><p>python3 操作 xmind 工具画思维导图 基础知识详解</p>
<p>首先安装 xmind 模块,安装代码如下:<code>pip install xmind</code></p>
<p>安装成功后导入包后即可操作,具体操作函数如下:</p>
<div class="jb51code">
<div><div id="highlighter_116578" class="syntaxhighlighter  py"><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="py keyword">import</code> <code class="py plain">xmind </code><code class="py comments">#加载包</code></div><div class="line number2 index1 alt1"><code class="py plain">w </code><code class="py keyword">=</code> <code class="py plain">xmind.load(</code><code class="py string">"test.xmind"</code><code class="py plain">) </code><code class="py comments"># 加载,如果不存在,创建新的工作布</code></div><div class="line number3 index2 alt2"><code class="py plain">s1</code><code class="py keyword">=</code><code class="py plain">w.getPrimarySheet </code><code class="py comments"># 得到第一页</code></div><div class="line number4 index3 alt1"><code class="py plain">s1.setTitle(</code><code class="py string">"first sheet"</code><code class="py plain">) </code><code class="py comments"># 给第一页命名</code></div><div class="line number5 index4 alt2">&nbsp;</div><div class="line number6 index5 alt1"><code class="py plain">r1</code><code class="py keyword">=</code><code class="py plain">s1.getRootTopic </code><code class="py comments"># 创建根节点</code></div><div class="line number7 index6 alt2"><code class="py plain">r1.setTitle(</code><code class="py string">"根"</code><code class="py plain">) </code><code class="py comments"># 给根节点命名</code></div><div class="line number8 index7 alt1">&nbsp;</div><div class="line number9 index8 alt2"><code class="py plain">`r2</code><code class="py keyword">=</code><code class="py plain">r1.addSubTopic</code><code class="py comments">#创建二级节点</code></div><div class="line number10 index9 alt1"><code class="py plain">r2.setTitle(</code><code class="py string">"枝叶"</code><code class="py plain">)</code><code class="py comments">#命名</code></div><div class="line number11 index10 alt2"><code class="py plain">xmind.save(</code><code class="py string">"test2.xmind"</code><code class="py plain">) </code><code class="py comments">##保存文件` </code></div></div></td></tr></tbody></table></div></div>
<div class="codetool" id="codetool"><a href="javascript:;" class="selall" title="全选"></a><a href="javascript:;" class="copy" title="复制python代码"></a><div class="code_n"><textarea></textarea></div></div></div>
<h2>画思维导图实例</h2>
<p>以下就是 Python 操作 Xmind 绘制脑图整体实例,详细操作如下:</p>
<div class="jb51code">
<div><div id="highlighter_694686" class="syntaxhighlighter  py"><div class="toolbar"><span><a href="#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="py keyword">import</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="py plain">w </code><code class="py keyword">=</code> <code class="py plain">xmind.load(</code><code class="py string">"test.xmind"</code><code class="py plain">)</code></div><div class="line number4 index3 alt1"><code class="py plain">s1 </code><code class="py keyword">=</code> <code class="py plain">w.getPrimarySheet </code></div><div class="line number5 index4 alt2"><code class="py comments"># 获取此工作表</code></div><div class="line number6 index5 alt1"><code class="py plain">s1.setTitle(</code><code class="py string">"first sheet"</code><code class="py plain">) </code></div><div class="line number7 index6 alt2"><code class="py comments"># 设置标题</code></div><div class="line number8 index7 alt1"><code class="py plain">a </code><code class="py keyword">=</code> <code class="py plain">{</code><code class="py string">"h1"</code><code class="py plain">: </code><code class="py string">'Python 技术学习'</code><code class="py plain">, </code><code class="py string">'h2'</code><code class="py plain">: [</code><code class="py string">'Python基础'</code><code class="py plain">, </code></div><div class="line number9 index8 alt2"><code class="py string">'Python 爬虫'</code><code class="py plain">],</code></div><div class="line number10 index9 alt1"><code class="py string">'h3'</code><code class="py plain">: [[</code><code class="py string">'Python环境安装'</code><code class="py plain">, </code><code class="py string">'Python基础语法'</code><code class="py plain">, </code><code class="py string">'Python数据结构'</code><code class="py plain">],</code></div><div class="line number11 index10 alt2"><code class="py spaces">&nbsp;</code><code class="py plain">[</code><code class="py string">'Python爬虫基础知识详解'</code><code class="py plain">, </code><code class="py string">'Python爬虫相关模块详解'</code><code class="py plain">]]}</code></div><div class="line number12 index11 alt1"><code class="py plain">r1 </code><code class="py keyword">=</code> <code class="py plain">s1.getRootTopic </code></div><div class="line number13 index12 alt2"><code class="py comments"># 获取此工作表的根主题</code></div><div class="line number14 index13 alt1"><code class="py plain">r1.setTitle(a[</code><code class="py string">'h1'</code><code class="py plain">]) </code></div><div class="line number15 index14 alt2"><code class="py comments"># 设置标题</code></div><div class="line number16 index15 alt1"><code class="py plain">c </code><code class="py keyword">=</code> <code class="py plain">a[</code><code class="py string">'h2'</code><code class="py plain">]</code></div><div class="line number17 index16 alt2"><code class="py plain">c2 </code><code class="py keyword">=</code> <code class="py plain">a[</code><code class="py string">'h3'</code><code class="py plain">]</code></div><div class="line number18 index17 alt1"><code class="py keyword">for</code> <code class="py plain">i, val </code><code class="py keyword">in</code> <code class="py functions">enumerate</code><code class="py plain">(c):</code></div><div class="line number19 index18 alt2"><code class="py functions">print</code><code class="py plain">(i, val)</code></div><div class="line number20 index19 alt1"><code class="py plain">a </code><code class="py keyword">=</code> <code class="py string">'b'</code> <code class="py keyword">+</code> <code class="py functions">str</code><code class="py plain">(i)</code></div><div class="line number21 index20 alt2"><code class="py plain">a </code><code class="py keyword">=</code> <code class="py plain">r1.addSubTopic</code></div><div class="line number22 index21 alt1"><code class="py plain">a.setTitle(val) </code><code class="py comments"># 设置标题</code></div><div class="line number23 index22 alt2"><code class="py keyword">for</code> <code class="py plain">i2, val2 </code><code class="py keyword">in</code> <code class="py functions">enumerate</code><code class="py plain">(c2):</code></div><div class="line number24 index23 alt1"><code class="py keyword">if</code> <code class="py plain">i </code><code class="py keyword">=</code><code class="py keyword">=</code> <code class="py plain">i2:</code></div><div class="line number25 index24 alt2"><code class="py plain">a2 </code><code class="py keyword">=</code> <code class="py string">'b2'</code> <code class="py keyword">+</code> <code class="py functions">str</code><code class="py plain">(i)</code></div><div class="line number26 index25 alt1"><code class="py plain">a2 </code><code class="py keyword">=</code> <code class="py plain">a.addSubTopic</code></div><div class="line number27 index26 alt2"><code class="py comments"># if isinstance(val, list):</code></div><div class="line number28 index27 alt1"><code class="py keyword">for</code> <code class="py plain">i3, val3 </code><code class="py keyword">in</code> <code class="py functions">enumerate</code><code class="py plain">(val2):</code></div><div class="line number29 index28 alt2"><code class="py plain">a3 </code><code class="py keyword">=</code> <code class="py string">'b3'</code> <code class="py keyword">+</code> <code class="py functions">str</code><code class="py plain">(i3)</code></div><div class="line number30 index29 alt1"><code class="py plain">a3 </code><code class="py keyword">=</code> <code class="py plain">a2.addSubTopic</code></div><div class="line number31 index30 alt2"><code class="py plain">a3.setTitle(val3)</code></div><div class="line number32 index31 alt1">&nbsp;</div><div class="line number33 index32 alt2"><code class="py plain">`xmind.save(w, </code><code class="py string">"Python_detail.xmind"</code><code class="py plain">)` </code></div></div></td></tr></tbody></table></div></div>
<div class="codetool" id="codetool"><a href="javascript:;" class="selall" title="全选"></a><a href="javascript:;" class="copy" title="复制python代码"></a><div class="code_n"><textarea></textarea></div></div></div>
<p>绘制的脑图显示如下:</p>
<p style="text-align: center"><img alt="图片.png" src="//img.jbzj.com/file_images/article/202110/2021102816381397.png" style="width: 800px; height: auto;"></p>
<p>到此这篇关于用 Python 操作 xmind 绘制思维导图的文章就介绍到这了,更多相关Python xmind 思维导图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!</p>
                        </div>
'''

def getPics(str):
    pics = re.compile(picPattern, re.I).findall(str)
    return pics


def formatPics(pics):
    pattern = r'>(?=((?!>).)*$)'
    altPattern = r'alt=[\"|\'].*?[\"|\']'
    stylePattern = r'style=[\"|\'].*?[\"|\']'
    i = 0
    for pic in pics:
        pic = re.sub(pattern, '/>', pic)
        pic = re.sub(altPattern, '', pic)
        # pic = re.sub(stylePattern, '', pic)
        pics[i] = pic
        i = i + 1
    return pics


def replacePics(repl, str):
    str = re.sub(picPattern, repl, str, flags=re.I)
    return str


def recoverPics(str, pics, recover):
    for pic in pics:
        str = re.sub(recover, pic, str, count=1, flags=re.I)
    return str




if __name__ == '__main__':
    pics = getPics(str)
    print(pics)

    print('----------------------------------------------')

    pics = formatPics(pics)
    print(pics)

    print('++++++++++++++++++++++++++++++++++++++++++++++')

    repl = r'my name'
    str = replacePics(repl, str)
    print(str)

    print('==============================================')

    recover = r'my.{1}?name'
    str = recoverPics(str, pics, recover)
    print(str)