Python小程序之sed命令替换
需求:
编写sed命令脚本
代码如下
# Author:Lee Sir import sys,os des_file = r'E:\StartPython\day3\test.txt'
des_file1 = r'E:\StartPython\day3\test1.txt' parameter = ['','Somehow','',des_file] def usage(parameter):
if len(parameter) == 4:
if isinstance(parameter[1],str) and isinstance(parameter[2],str):
old_str = parameter[1]
new_str = parameter[2]
if os.path.exists(parameter[3]):
des_file = parameter[3]
return True, old_str, new_str, des_file
return False def check_string_exist(old,file):
with open(file,encoding='utf-8') as fd:
for line in fd:
if old not in line:
return False
else:
return True def replace(old,new,file):
with open(file,'r+',encoding='utf-8') as fd,open(des_file1,'w+',encoding='utf-8') as fd1:
for line in fd:
if old in line:
new_line = line.replace(old,new)
else:
new_line = line
print(new_line)
fd1.write(new_line) def main():
result = usage(parameter)
if result:
if check_string_exist(result[1],result[3]):
replace(result[1],result[2],result[3])
else:
print('the %s is not found in %s ' % (result[1],result[3]))
else:
exit('USAGE: %s old_str new_str des_file' % sys.argv[0]) if __name__ == '__main__':
main()
Python小程序之sed命令替换的更多相关文章
- Day1:第一个python小程序
Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...
- 怎么样通过编写Python小程序来统计测试脚本的关键字
怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...
- Python 小程序,对文件操作及其它
以下是自己写的几个对文件操作的小程序,里面涉及到文件操作,列表(集合,字典)的运用等.比方说,从文件里读取一行数据.分别存放于列表中,再对列表进行操作.如去掉里面的反复项.排序等操作. 常见对文件里行 ...
- 【转】我的第一个Python小程序
原文网址:http://blog.csdn.net/randyqiu/article/details/4484089 人的每个第一次都有点特别的意义,所以下面这个小程序我把他记录下来做个纪念. 因为要 ...
- 一个有意思的Python小程序(全国省会名称随机出题)
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近比较迷Python,仿照<Python编程快速上手>8.5写了一个随机出卷的小 ...
- 在shell中使用sed命令替换/为\/
sed命令相关: https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html https://www.cnblogs.com/D ...
- 写一个python小程序
在windows环境下进行操作 window+R 输入cmd 创建一个文件夹 mkdir pytxt 创建一个py文件 py.py 用notepad或者记事本等工具进行编辑 或 首先声明pytho ...
- 人人都可以写的可视化Python小程序第二篇:旋转的烟花
兴趣是最好的老师 枯燥的编程容易让人放弃,兴趣才是最好的老师.无论孩子还是大人,只有发现这件事情真的有趣,我们才会非常执着的去做这件事,比如打游戏.如果编程能像玩游戏一样变得有趣,我相信很多人就特别愿 ...
- sed命令替换文件内容
reference: https://www.cnblogs.com/starof/p/4181985.html 抓取目录名并修改 ls | grep "XXX" > 1.t ...
随机推荐
- swagger webapi控制器注释不显示
swagger是webapi文档描述及调试工具,要在asp.net mvc中使用swagger,需要安装Swashbuckle.Core这个包,安装好后会在app_start中生成SwaggerCon ...
- Ubuntu下使用Git_4
在这个第四个文章中,我将练习GIT的高级阶段了,由于高级阶段的内容转的比较多,我自己的代码除了我自己可以看懂意外,大家可能看不懂,所以我将会按照 http://git.wiki.navisec.it/ ...
- 第二十二篇 正在表达式 re模块
re模块****** 就本质而言,正则表达式时一种小型的,高度专业化的编程语言,在python里,它内嵌在python中,并通过re模块实现.正则表达式模式被编译成一系列的字节码.然后用C编写的匹配引 ...
- Struts2(六.用标签显示用户列表及Value Stack和Stack Context)
一.用Struts2标签显示用户列表 原理: 在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问 原则: 在jsp网页上,尽量不要出现 ...
- mysql分布式技术
所有的分布式技术 dobble zokkiper ngix
- remix无法安装的解决方案
无法安装的原因: 因为remix依赖python 执行python又依赖c++的环境 所以连环导致出错 https://github.com/nodejs/node-gyp 措施一:降级处理 先清理缓 ...
- POJ 2186 Popular Cows(强联通+缩点)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- DFS——hdu5682zxa and leaf
一.题目回顾 题目链接:zxa and leaf Sample Input 2 3 2 1 2 1 3 2 4 3 9 6 2 1 2 1 3 1 4 2 5 2 6 3 6 5 9 Sample ...
- Hibernate配置实体类的属性
Hibernate配置实体类的属性既可以在页面显示关联实体类的所有属性,在插入该属性时又可以只插入单一属性 private String companyCode; private CompanyEnt ...
- Mac上基于hexo+GitHub搭建个人博客(一)
原文地址: http://fanjiajia.cn/2018/11/23/Mac%E4%B8%8A%E5%9F%BA%E4%BA%8Ehexo+GitHub%E6%90%AD%E5%BB%BA%E4% ...