设为首页 加入收藏

TOP

Python-Day4实现简单的shell sed替换功能
2017-09-30 12:55:49 】 浏览:8418
Tags:Python-Day4 实现 简单 shell sed 替换 功能

code:

 1 f = open('yesterday','r',encoding='utf-8')
 2 f2 = open('yesterday.bak','w',encoding='utf-8')
 3 old_str = input('请输入要修改的字符:')
 4 replace_str = input('请输入替换成的字符:')
 5 for line in f.readlines():
 6     line = line.replace(old_str,replace_str)
 7     print(line)
 8     f2.write(line)
 9 f.close()
10 f2.close()

文件内容:(yesterday)

Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露

 

感想:

  思路很简单就是打开源文件,然后循环,把源文件要替换的内容替换再写入新文件!

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇洗礼灵魂,修炼python(2)--pyth.. 下一篇洗礼灵魂,修炼python(3)--从一..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目