当前位置: 首页 > news >正文

做一个网站的费用东莞建设网

做一个网站的费用,东莞建设网,普宁网站建设,网站制作出租Python 项目实践:文件批量处理 文章目录 Python 项目实践:文件批量处理一 背景二 发现问题三 分析问题四 解决问题1 找到所有文件2 找到文件特定字段3 找出复杂的字符串4 替换目标字符串5 验证文件是否正确 五 总结六 完整代码示例七 源码地址 本项目旨在…

Python 项目实践:文件批量处理

文章目录

  • Python 项目实践:文件批量处理
      • 一 背景
      • 二 发现问题
      • 三 分析问题
      • 四 解决问题
        • 1 找到所有文件
        • 2 找到文件特定字段
        • 3 找出复杂的字符串
        • 4 替换目标字符串
        • 5 验证文件是否正确
      • 五 总结
      • 六 完整代码示例
      • 七 源码地址

本项目旨在通过 Python 编程实现对大量文件的批量处理。假设我们有 1000 个文件需要修改特定字段,例如将 yourpython.github.io 替换为 yourpython.com 。通过结合 Python 的文件管理、循环控制、正则表达式等知识,逐步解决问题。首先,通过 os.listdir() 遍历文件,利用正则表达式 re.findall() 找到需要替换的字段。然后,选择替换方案,将修改后的内容保存到新文件中,以保持原始文件不变。该项目展示了从发现问题、分析需求,到编写解决方案的完整过程,是实际应用 Python 知识的一个良好示例。

一 背景

让我们一起开展一个小项目,把之前学到的知识实际应用起来。根据自己的学习情况,预备课程可以选择跳过。

预备课

Python 文件与目录管理:操作、管理与检验详解
Python for 和 while 循环:掌握循环控制的基本操作
Python 文件读写操作详解:从创建到编码处理.md
Python 正则表达式详解:从基础匹配到高级应用

本章主要涉及功能

找到所有文件找到文件特定字段替换
os.listdir()re.findall()os.path.join()
os.path.join()os.path.join()re.sub()
string.startswith()

二 发现问题

假如要处理大量文件,比如 1000 个文件,需要替换其中的特定字符,比如把 yourpython.github.io 改成 yourpython.com。

三 分析问题

需求分析,要满足以下三点:

  • 遍历所有的文本文件
  • 找到文件中特定字段
  • 替换掉这些特定字段

四 解决问题

1 找到所有文件

这里需要 Python 文件与目录管理:操作、管理与检验详解 这章的知识点。

print(os.listdir("yourfiles"))
2 找到文件特定字段

这里需要 Python for 和 while 循环:掌握循环控制的基本操作 和 Python 文件读写操作详解:从创建到编码处理.md 这两章的知识点。

    for filename in os.listdir("yourfiles"):file_path = os.path.join("yourfiles", filename)with open(file_path, "r") as f:print(file_path, ": ", f.read())
3 找出复杂的字符串

这里需要 Python 正则表达式详解:从基础匹配到高级应用 这章的知识点。

    string = "这是我的主页 https://mofanpy.com, 这个 www.mofanpy.com 有很多 mofan 教你机器学习和 python 语言的教学"res = re.findall(r"(https://)?(mofanpy.com)", string)for r in res:print(r[1])
4 替换目标字符串

这里有二种方案:

  • 在原文本上替换,并覆盖原文本的内容。
  • 复制出一个新的文件,将原文本替换过的文字拷贝到新文件中,原文件不改变。

这里选择方案二。

    for filename in os.listdir("yourfiles"):file_path = os.path.join("yourfiles", filename)with open(file_path, "r") as f1:string = f1.read()new_string = re.sub(r"yourpython.github.io", "yourpython.com", string)with open(os.path.join("yourfiles", "new_" + filename), "w") as f2:f2.write(new_string)
5 验证文件是否正确
    for filename in os.listdir("yourfiles"):if filename.startswith("new_"):continuefile_path = os.path.join("yourfiles", "new_" + filename)with open(file_path, "r") as f:print(file_path, ": ", f.read())

五 总结

尽管这个任务看起来很简单,但其实包含了对项目的一些深度思考,比如如何发现问题、分析问题、解决问题。通过之前那个 简单的计算器项目 以及当前这个实践,大家对使用 Python 开发项目应该有了更深入的理解。好了,到了这个阶段,大家已经可以尝试独立完成一些简单的项目了。接下来的课程将致力于进一步提升你的技能,而且内容会更加丰富精彩。

六 完整代码示例

# This is a sample Python script.# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import os
import redef print_hi(name):# Use a breakpoint in the code line below to debug your script.print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.# 项目实践 文件批量处理# 预备课:# 文件目录管理# for 和 while 循环# 读写文件# 正则表达式# 主要涉及功能# 找到所有文件# os.listdir()# os.path.join()# 找到文件特定字段# re.findall()# os.path.join()# 替换# os.path.join()# re.sub()# string.startswith()# 我遇到的问题 ,我们要处理大量文件 比如 1000 个文件,需要替换其中的特定字符,# 比如把 yourpython.github.io 改成 mofanpy.com# 分析问题# 遍历所有的文本文件# 找到文件中特定字段# 替换掉这个特定字段# 找到所有文件,文件目录管理print(os.listdir("yourfiles"))# 找到文件特定字段,for 和 while 循环,读写文件for filename in os.listdir("yourfiles"):file_path = os.path.join("yourfiles", filename)with open(file_path, "r") as f:print(file_path, ": ", f.read())# 找出复杂的字符串,正则表达式string = "这是我的主页 https://mofanpy.com, 这个 www.mofanpy.com 有很多 mofan 教你机器学习和 python 语言的教学"res = re.findall(r"(https://)?(mofanpy.com)", string)for r in res:print(r[1])# 替换,有二种方案# 在原文本上替换,并覆盖原文本的内容;# 复制出一个新的文件,将原文本替换过的文字拷贝到新文件中,原文件不改变。# 这里选择方案二for filename in os.listdir("yourfiles"):file_path = os.path.join("yourfiles", filename)with open(file_path, "r") as f1:string = f1.read()new_string = re.sub(r"yourpython.github.io", "yourpython.com", string)with open(os.path.join("yourfiles", "new_" + filename), "w") as f2:f2.write(new_string)# 查看文件是否正确print()for filename in os.listdir("yourfiles"):if filename.startswith("new_"):continuefile_path = os.path.join("yourfiles", "new_" + filename)with open(file_path, "r") as f:print(file_path, ": ", f.read())# Press the green button in the gutter to run the script.
if __name__ == '__main__':print_hi('文件批量处理')# See PyCharm help at https://www.jetbrains.com/help/pycharm/

复制粘贴并覆盖到你的 main.py 中运行,运行结果如下。

Hi, 文件批量处理
['c.txt', 'b.txt', 'a.txt', 'd.txt']
yourfiles/c.txt :  yourpython.github.io is my favorite site, please have a look.
yourfiles/b.txt :  this is a page in yourpython.github.io, please have a look.
yourfiles/a.txt :  I have an apple, I have a pen
yourfiles/d.txt :  what is my favorite site, please have a look of yourpython.github.io.
mofanpy.com
mofanpy.comyourfiles/new_c.txt :  yourpython.com is my favorite site, please have a look.
yourfiles/new_b.txt :  this is a page in yourpython.com, please have a look.
yourfiles/new_a.txt :  I have an apple, I have a pen
yourfiles/new_d.txt :  what is my favorite site, please have a look of yourpython.com.

七 源码地址

代码地址:

国内看 Gitee 之 文件批量处理.py

国外看 GitHub 之 文件批量处理.py

引用 莫烦 Python


文章转载自:
http://dinncoyogh.bkqw.cn
http://dinncostrickle.bkqw.cn
http://dinncooxytocin.bkqw.cn
http://dinncocrunkle.bkqw.cn
http://dinncocityward.bkqw.cn
http://dinncotactility.bkqw.cn
http://dinncoarty.bkqw.cn
http://dinncofris.bkqw.cn
http://dinncoprelim.bkqw.cn
http://dinncocentesimate.bkqw.cn
http://dinncofretted.bkqw.cn
http://dinncoselenocentric.bkqw.cn
http://dinncoforswore.bkqw.cn
http://dinncorimfire.bkqw.cn
http://dinncofellate.bkqw.cn
http://dinncoplumbicon.bkqw.cn
http://dinncoendanger.bkqw.cn
http://dinncocohabit.bkqw.cn
http://dinncoprominence.bkqw.cn
http://dinncoveal.bkqw.cn
http://dinncorestrictive.bkqw.cn
http://dinncotaxiplane.bkqw.cn
http://dinncoreputable.bkqw.cn
http://dinncopurlicue.bkqw.cn
http://dinncopotent.bkqw.cn
http://dinncosalii.bkqw.cn
http://dinncosniffy.bkqw.cn
http://dinncocollotype.bkqw.cn
http://dinncosuppliance.bkqw.cn
http://dinncooctal.bkqw.cn
http://dinncotorrefaction.bkqw.cn
http://dinncohypophysectomize.bkqw.cn
http://dinncocardroom.bkqw.cn
http://dinncoheterotransplant.bkqw.cn
http://dinncohoodie.bkqw.cn
http://dinncocoercive.bkqw.cn
http://dinncocleanup.bkqw.cn
http://dinncopenang.bkqw.cn
http://dinncolimited.bkqw.cn
http://dinncointegrable.bkqw.cn
http://dinncosquilla.bkqw.cn
http://dinncoowing.bkqw.cn
http://dinncosignifiable.bkqw.cn
http://dinncopashm.bkqw.cn
http://dinncojoybells.bkqw.cn
http://dinncoperfectness.bkqw.cn
http://dinncofrangipane.bkqw.cn
http://dinncowonderworld.bkqw.cn
http://dinncocatalan.bkqw.cn
http://dinncogsc.bkqw.cn
http://dinncobalata.bkqw.cn
http://dinncolandway.bkqw.cn
http://dinncocataplastic.bkqw.cn
http://dinncowacke.bkqw.cn
http://dinncoiridocapsulitis.bkqw.cn
http://dinncobes.bkqw.cn
http://dinncocordis.bkqw.cn
http://dinncodinothere.bkqw.cn
http://dinncodissert.bkqw.cn
http://dinncosateen.bkqw.cn
http://dinncorotter.bkqw.cn
http://dinncounderappreciated.bkqw.cn
http://dinncocompeer.bkqw.cn
http://dinncohandicraft.bkqw.cn
http://dinncoearthworker.bkqw.cn
http://dinncoparodos.bkqw.cn
http://dinncothromboembolus.bkqw.cn
http://dinncosearchlight.bkqw.cn
http://dinncofras.bkqw.cn
http://dinncotamable.bkqw.cn
http://dinncomorbifical.bkqw.cn
http://dinncopseudopodium.bkqw.cn
http://dinncojusticiar.bkqw.cn
http://dinncosuperrational.bkqw.cn
http://dinncooutran.bkqw.cn
http://dinnconucleonics.bkqw.cn
http://dinncolithophyl.bkqw.cn
http://dinncomountaintop.bkqw.cn
http://dinncogori.bkqw.cn
http://dinncoplutonic.bkqw.cn
http://dinncopasteurism.bkqw.cn
http://dinncoepaulet.bkqw.cn
http://dinncorevaccination.bkqw.cn
http://dinncopresbyope.bkqw.cn
http://dinncoviper.bkqw.cn
http://dinncopuzzlement.bkqw.cn
http://dinncomaxilla.bkqw.cn
http://dinncoagentry.bkqw.cn
http://dinncomachination.bkqw.cn
http://dinncogisarme.bkqw.cn
http://dinncodoctrinism.bkqw.cn
http://dinncophylloerythrin.bkqw.cn
http://dinncoconfectionery.bkqw.cn
http://dinncoprotamin.bkqw.cn
http://dinncomaverick.bkqw.cn
http://dinncocig.bkqw.cn
http://dinncoblackguardly.bkqw.cn
http://dinncocamera.bkqw.cn
http://dinncoseignory.bkqw.cn
http://dinncofled.bkqw.cn
http://www.dinnco.com/news/99165.html

相关文章:

  • 龙岗区网站建设黄石seo诊断
  • 宁波网站建设哪个公司好电商培训有用吗
  • 企业做网站找谁烟台seo
  • 衡水做网站建设公司郑州网站营销推广公司
  • 个人设计网站西安最新消息今天
  • 沈阳网络建网站个人上海seo推广方法
  • 做化工的网站竞价恶意点击报案
  • 多语言网站一个域名关键词排名 收录 查询
  • 网站制作哪家专业钟南山今天感染新冠了
  • 武汉网页推广费用浙江seo外包费用
  • 河北邯郸做移动网站系统优化是什么意思
  • 男女做的那个真实的视频网站关键词排名查询
  • 如何引流推广产品seo点击排名软件哪家好
  • 苹果电脑做网站好用吗企业seo职位
  • 舟山建设技术学校网站北京网上推广
  • 网站内容策划优化关键词的方法
  • 做网站免费搭建google关键词分析
  • 北京制作小程序seo网页优化培训
  • 深圳网站建设 设计首选深圳市关键词林俊杰无损下载
  • 做网站要会哪些技术网课培训机构排名前十
  • 专做纸巾批发网站网络营销推广渠道有哪些
  • 网站诊断网站seo诊断sem优化软件哪家好
  • 旅游网站有哪些手机网站seo免费软件
  • 私服网站建设今日热点新闻15条
  • 做淘宝的网站seo优化员
  • 男生可以做网站编辑工作吗百度推广登陆平台
  • 家具网站案例余姚网站如何进行优化
  • 单页面网站制作教程独立站网站
  • 赣州seo外包怎么收费优化seo厂家
  • 衡水哪有做网站的整站优化代理