博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3-1 实现简单的shell sed替换功能
阅读量:4672 次
发布时间:2019-06-09

本文共 2426 字,大约阅读时间需要 8 分钟。

 

1.需求

程序1: 实现简单的shell sed替换功能

  file1 的内容copy到file2

  输入参数./sed.py  $1  $2  

    $1替换成$2 (把a替换成% )

 

2.个人思路

open file 1 2file1 内容 copy 到 file2read 每一行 ,        if a in line:                   a替换%        关闭file1 file2

  

代码

f1 = open('yes.txt','r+',encoding='utf-8')f_new = open('yes2.txt','w',encoding='utf-8')for line in f1.readlines():    line = line.replace('a','%').strip()    print(line)    f_new.writelines(line)f1.close()f_new.close()

  

 

 

3.个人心得

 

3.1 读取文件

  方法1:记得f1.close()  f_new.close()

f1 = open('yes.txt','r+',encoding='utf-8')f_new = open('yes2.txt','w',encoding='utf-8')

  

  方法2:自动帮你关闭文件

with open('yes.txt','r+',encoding='utf-8') as f1:    with open('yes2.txt','w',encoding='utf-8') as f_new:            for line in f1.readlines():

  

3.2  copy全部

方法1:f1 的内容copy到f_new

#全文复制
f1 = open('yes.txt','r+',encoding='utf-8') f2 = open('yes2.txt','w',encoding='utf-8')
for line in f1.readlines():   print(line.strip())   f_new.writelines(line) #此时光标已经到末尾了

  

 方法2:shutil模块(文本处理,压缩)

import shutilshutil.copyfile("yes.txt","yes2.txt")

 

3.3 文件替换

 读取  f1 的每行,a替换成%,并且写入到f_new  

f1 = open('yes.txt','r+',encoding='utf-8')f2 = open('yes2.txt','w',encoding='utf-8')for line in f1.readlines():    line = line.replace('a','%').strip()    print(line)    f_new.writelines(line)

  

3.4 光标问题

注意:全文复制,(读取每一行,然后copy到 f2 ),此时光标已经到文件末尾!  执行替换时,已经读取不到内容

错误代码

# coding=utf-8#打开文件f1 = open('yes.txt','r+',encoding='utf-8')f_new = open('yes2.txt','w',encoding='utf-8')#全文复制for line in f1.readlines():    print(line.strip())    f_new.writelines(line)#光标已经到末尾了a = f1.readlines() # a[]#替换for line in f1.readlines():    line = line.replace("a",'%')    print(line.strip())    f_new.writelines(line)

 

3.5 sys模块 传入参数

传入参数 sys.argv[1]

import sys#打开文件with open('yes.txt','r+',encoding='utf-8') as f1:    with open('yes2.txt','w',encoding='utf-8') as f_new:        find_str = sys.argv[1]        replace_str = sys.argv[2]

 

cmd 执行代码时,可以带参数 

D:\PycharmProjects\s14\作业>python "3-1 shell sed功能2.1.py" a %

  

 

4. 完整代码

# coding=utf-8import sys#打开文件with open('yes.txt','r+',encoding='utf-8') as f1:    with open('yes2.txt','w',encoding='utf-8') as f_new:        find_str = sys.argv[1]        replace_str = sys.argv[2]        #替换        for line in f1.readlines():            line = line.replace(find_str,replace_str).strip()            print(line)            f_new.writelines(line)        f1.close()        f_new.close()

  

 

转载于:https://www.cnblogs.com/venicid/p/7246112.html

你可能感兴趣的文章
PHP面向对象:instanceof 运算符 (备忘)
查看>>
数据存储-CoreData总结
查看>>
通过Ajax的方式执行GP服务
查看>>
Ztree加载完成后显示勾选节点
查看>>
HDU 3401
查看>>
asp.net中XmlDocument解析出现出错,处理特殊字符
查看>>
unable to locate package gparted
查看>>
Centos7安装Mysql
查看>>
Hadoop伪分布安装配置
查看>>
idhttp.post方式 调用datasnap rest 远程方法(转咏南兄)
查看>>
jQuery包裹节点用法完整示例
查看>>
EL表达式中,param和requestScope的区别
查看>>
AngularJs angular.element
查看>>
利用HUtool读取Excel内容
查看>>
浅析libuv源码-node事件轮询解析(1)
查看>>
JS——try catch throw
查看>>
Python 学习2
查看>>
在Recyclerview使用GlideAPP加载大量图片导致内存溢出(oom)
查看>>
js代码格式化工具(格式化、压缩、加密压缩)
查看>>
HTML特殊符号
查看>>