1. fp = open(''test2.txt','w') #打开你要写得文件test2.txt
  2. lines = open('test1.txt').readlines() #打开文件,读入每一行
  3. for s in lines:
  4. fp.write( s.replace('love','hate').replace('yes','no')) # replace是替换,write是写入
  5. fp.close() # 关闭文件
  1. import os
  2. import re
  3. f_path = r'c:\a.txt'
  4. f = open (f_path, "r+")
  5. open('c:\\test.txt', 'w').write(re.sub(r'hello world', 'Love python', f.read()))

原文: 
参考备忘,指针这个没明白什么意思,找时间验证下

  1. #写在原文件中
  2. fp3=open("file3.txt","r+") #不用w w会清空数据
  3. s=fp3.read()#读出
  4. fp3.seek(0,0) #指针移到头 原来的数据还在是替换 会存在一个问题 如果少 会替换不了全部数据,自已思考解决!
  5. #从头写入
  6. fp3.write(s.replace("hello","good"))
  7. fp3.close()
  1. import os
  2. os.chdir('D:\\') # 跳到D盘
  3. if not os.path.exists('test1.txt'): # 看一下这个文件是否存在
  4. exit(-1) #不存在就退出
  5. lines = open('test1.txt').readlines() #打开文件,读入每一行
  6. fp = open(''test2.txt','w') #打开你要写得文件test2.txt
  7. for s in lines:
  8. # replace是替换,write是写入
  9. fp.write( s.replace('love','hate').replace('yes','no'))
  10. fp.close() # 关闭文件

python按行读取并替换的更多相关文章

  1. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  2. Python按行读取文件

    1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...

  3. ZH奶酪:Python按行读取文件

    1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...

  4. python按行读取apk中版本号、包名等信息

    主要是读apk中manifest.xml中的信息. 读单一apk信息:见“文件”中“apkInfo.xml”.实际运行时,需要将后缀由“.xml”改为“.py". 批量自动获取apk软件信息 ...

  5. python 按行读取判断是否为空

    for line in fr.readlines(): try: # print(len(line)) if(len(line)==1): continue else: fw.write(matche ...

  6. python按行读取文件,如何去掉换行符"\n"

    for line in file.readlines(): line=line.strip('\n')

  7. python_基础学习_01_按行读取文件的最优方法

    python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...

  8. C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...

  9. Python跳过第一行读取文件内容

    Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...

随机推荐

  1. ABAP-HTML浏览器

  2. inotify用法简介及结合rsync实现主机间的文件实时同步

    一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...

  3. Cookie进行会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  4. debug、release

    1.区别 Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug和Release的行为完全颠 ...

  5. 7.Reverse Integer (INT; Overflow)

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:要注意溢出 ...

  6. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  7. memcache缓存失效

    缓存过期 memcached在处理过期的缓存项时,采用懒惰模式处理方法. 缓存项过期,不会立即删除,直到对该缓存项执行了get操作,才会删除过期缓存. > set key 0 10 > t ...

  8. Django配置Bootstrap, js

    1.首先在APP目录下创建一个static文件夹 如图: # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'dj ...

  9. 用递归方法求 n!

    #include <iostream> using namespace std; #define LL long long LL fac(int n) { LL f; || n == ) ...

  10. Codeforces 612B. Wet Shark and Bishops 模拟

    B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...