问题描述
- 修改TXT文件中部分内容
-
Id: 1
ASIN: 0827229534
title: Patterns of Preaching: A Sermon Sampler
group: Book
salesrank: 396585
similar: 5 0804215715 156101074X 0687023955 0687074231 082721619X
categories: 2
|Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Christianity[12290]|Clergy[12360]|Preaching[12368]
|Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Christianity[12290]|Clergy[12360]|Sermons[12370]
reviews: total: 2 downloaded: 2 avg rating: 5
2000-7-28 cutomer: A2JW67OY8U6HHK rating: 5 votes: 10 helpful: 9
2003-12-14 cutomer: A2VE83MZF98ITY rating: 5 votes: 6 helpful: 5
以上是我的m.txt文件中的内容,我想将该文件中的avg rating:替换为avg:求Python具体代码
解决方案
with open('m.txt', 'r') as f1:
txt = f1.read()
txt.replace('avg rating:', 'avg:')
with open('m1.txt' , 'w') as f2:
f2.write(txt)
解决方案二:
用c修改文件中部分内容
解决方案三:
读取文件所有内容到字符串pp,
pp.replace函数进行字符串替换即可
解决方案四:
使用python的读文件(http://www.jb51.net/article/45956.htm),读取出来之后替换(http://blog.csdn.net/cnmilan/article/details/8727497),替换后的字符串写入一个副本中保存
解决方案五:
···
with open('m.txt', 'r') as f1:
txt = f1.read()
txt.replace('avg rating:', 'avg:')
with open('m1.txt' , 'w') as f2:
f2.write(txt)
···
时间: 2024-09-14 17:31:37