Commit 1b6aa9b7 by BellCodeEditor

save project

parent 7e3a2ff3
Showing with 29 additions and 1 deletions
# 写入同学们的捐赠明细:'小兰:12本'、'小丽:11本'、'李文:9本'、'张伟:16本' # 使用Python的文件方法,创建一个名为test1.txt的文件,并在文件中添加任意内容。
#注意 开-写-关
# f=open(r"C:\Users\Administrator\Desktop\顾博淇\test.txt","w",encoding="utf-8")
# f.write("小兰,12")
# f.close()
#在文件中追加数据
#使用with open()as 补充内容
# with open(r"C:\Users\Administrator\Desktop\顾博淇\test.txt","a",encoding="utf-8")as f:
# f.write("小狗,12\n")
# f.write("小象,12\n")
#读取文件数据,并打印在控制台a
w=''
with open(r"C:\Users\Administrator\Desktop\顾博淇\test.txt","r",encoding="utf-8")as f:
for i in f:
if "小象,12" in i:
print("在")
i=i.replace("小象,12","小象,13")
w=w+i
# print(w)
#使用for循环遍历读取文件的内容,并判断读取的内容是否在文件中。
with open(r"C:\Users\Administrator\Desktop\顾博淇\test.txt","w",encoding="utf-8")as f:
f.write(w)
#使用replace()方法修改文件中的数据
#用变量调整所要写入的数据 ,注意要重新写入
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment