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
d3af87c5
authored
Aug 26, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
49a2427d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
5 deletions
diy.py
diy.py
View file @
d3af87c5
...
...
@@ -3,12 +3,18 @@
#file1.write("小兰:12本'\n'李文:9本'\n'张伟:16本")
#file1.close()
with
open
(
r"C:\Users\HP\Desktop\新建文件夹\test.txt"
,
"r"
,
encoding
=
"utf-8"
)
as
file1
:
abc
=
""
with
open
(
r"C:\Users\HP\Desktop\新建文件夹\test.txt"
,
"w"
,
encoding
=
"utf-8"
)
as
file1
:
#把文件里面的内容遍历在控制台
#判断小兰:12本在不在里边,在的话打印“数据在里面”出控制台
for
i
in
file1
:
print
(
i
)
if
'小兰:12本'
in
file1
:
print
(
"数据在里面"
)
file1
.
write
(
"小兰:12本'
\n
'李文:9本'
\n
'张伟:16本"
)
for
data
in
file1
:
if
'小兰:12本'
in
data
:
data
=
data
.
replace
(
"小兰:12本"
,
"小兰:15本"
)
abc
+=
data
#创建空的字符串变量,修改里边的数据,最后能够打印出新的内容出来
with
open
(
r"C:\Users\HP\Desktop\新建文件夹\test.txt"
,
"w"
,
encoding
=
"utf-8"
)
as
file1
:
file1
.
write
(
abc
)
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