python按行读取并替换
fp = open(''test2.txt','w') #打开你要写得文件test2.txt
lines = open('test1.txt').readlines() #打开文件,读入每一行
for s in lines:
fp.write( s.replace('love','hate').replace('yes','no')) # replace是替换,write是写入
fp.close() # 关闭文件
import os
import re
f_path = r'c:\a.txt'
f = open (f_path, "r+")
open('c:\\test.txt', 'w').write(re.sub(r'hello world', 'Love python', f.read()))
原文:
参考备忘,指针这个没明白什么意思,找时间验证下
#写在原文件中
fp3=open("file3.txt","r+") #不用w w会清空数据
s=fp3.read()#读出
fp3.seek(0,0) #指针移到头 原来的数据还在是替换 会存在一个问题 如果少 会替换不了全部数据,自已思考解决!
#从头写入
fp3.write(s.replace("hello","good"))
fp3.close()
import os
os.chdir('D:\\') # 跳到D盘
if not os.path.exists('test1.txt'): # 看一下这个文件是否存在
exit(-1) #不存在就退出
lines = open('test1.txt').readlines() #打开文件,读入每一行
fp = open(''test2.txt','w') #打开你要写得文件test2.txt
for s in lines:
# replace是替换,write是写入
fp.write( s.replace('love','hate').replace('yes','no'))
fp.close() # 关闭文件
python按行读取并替换的更多相关文章
- Python按行读取文件、写文件
Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...
- Python按行读取文件
1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...
- ZH奶酪:Python按行读取文件
1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...
- python按行读取apk中版本号、包名等信息
主要是读apk中manifest.xml中的信息. 读单一apk信息:见“文件”中“apkInfo.xml”.实际运行时,需要将后缀由“.xml”改为“.py". 批量自动获取apk软件信息 ...
- python 按行读取判断是否为空
for line in fr.readlines(): try: # print(len(line)) if(len(line)==1): continue else: fw.write(matche ...
- python按行读取文件,如何去掉换行符"\n"
for line in file.readlines(): line=line.strip('\n')
- python_基础学习_01_按行读取文件的最优方法
python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...
- C++/Php/Python/Shell 程序按行读取文件或者控制台
写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...
- Python跳过第一行读取文件内容
Python编程时,经常需要跳过第一行读取文件内容.比较容易想到是为每行设置一个line_num,然后判断line_num是否为1,如果不等于1,则进行读取操作.相应的Python代码如下: inpu ...
随机推荐
- ABAP-关于隐式与显式的DB Commit
转载:https://www.cnblogs.com/liaojunbo/archive/2011/07/11/2103491.html 1.显式的DB Commit 显式的DB Commit并没有对 ...
- postgresql 的操作
基本操作: \o /tmp/11.txt ,查询结果输出到文件 \d 查询table结构 \x 切换显示方式 postgresql中可以导出某个sql的执行结果到文件中,方法是在psql中首先执行\o ...
- sql计算总页数
1 计算总页数方法: public int getTotalCount() { Statement stmt = null; //提交SQL语句对象stmt Resu ...
- a,b = b,a 换值问题
a = "hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhe ...
- neo4j 学习-2
Neo4j 查询例句 MATCH (john {name: 'John'})-[:friend]->()-[:friend]->(fof) RETURN john.name, fof.na ...
- JSP共享javabean
JavaBean是一种可重复使用,且跨平台的软件组件.JavaBean可分为两种:一种是有用户界面(UI)的javaBean:还有一种是没有用户界面,主要负责处理事务(如数据运算,操纵数据库)的jav ...
- ubuntu安装openssh-server 报依赖错误的解决过程
ubuntu自带的有openssh-client,所以可以通过 ? 1 ssh username@host 来远程连接linux 可是要想通过ssh被连接,ubuntu系统需要有openssh-ser ...
- KNN识别手写数字
一.问题描述 手写数字被存储在EXCEL表格中,行表示一个数字的标签和该数字的像素值,有多少行就有多少个样本. 一共42000个样本 二.KNN KNN最邻近规则,主要应用领域是对未知事物的识别,即判 ...
- distinct top執行順序
select distinct top 3 from table; 先distinct后top
- Java工具类_随机生成任意长度的字符串【密码、验证码】
import java.util.Random; public class PasswordCreate { /** * 获得密码 * @param len 密码长度 * @return */ pub ...