Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson12-1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1b6aa9b7
authored
Jun 26, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
7e3a2ff3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
1 deletions
diy.py
diy.py
View file @
1b6aa9b7
# 写入同学们的捐赠明细:'小兰: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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment