◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
如何使用 python 批量更改 markdown 文档中的图片地址
在 markdown 文档中,图片通常通过以下语法引用:

其中,图片描述和图片路径的内容可能包含汉字。
代码实现
立即学习“Python免费学习笔记(深入)”;
为了处理多个 markdown 文档中的图片地址,我们可以使用 python 和正则表达式:
import re def process_markdown_content(content): # 编译正则表达式以匹配 markdown 中的图片链接 img_pattern = re.compile(r'![([^]]*)]((./[^)]+))') # 定义一个替换函数,将旧图片路径替换为新的路径 def replacer(match): description = match.group(1) # 获取图片描述 old_path = match.group(2) # 获取旧图片路径 # 将旧路径替换为新路径 new_path = old_path.replace('./', './new_folder/') return f'' # 替换为新链接 # 使用替换函数替换匹配到的所有链接 new_content = img_pattern.sub(replacer, content) return new_content # 示例 markdown_content = ''' here is some markdown content.   ''' new_content = process_markdown_content(markdown_content) print(new_content)
输出:
Here is some markdown content.  
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。