Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson13-4
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
8f10a005
authored
Mar 20, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
0cc37ad4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
0 deletions
hello.py
wjdx.py
wjdx2.py
文件读写 二.py
hello.py
0 → 100644
View file @
8f10a005
a
=
[
'你好
\n
'
,
'悟空
\n
'
,
'欢迎来到贝尔编程
\n
'
]
with
open
(
r'c:\Users\10839\Desktop\hello.txt'
,
'w'
,
encoding
=
'utf-8'
)
as
file
:
file
.
writelines
(
a
)
\ No newline at end of file
wjdx.py
0 → 100644
View file @
8f10a005
with
open
(
r'C:\Users\10839\Desktop\sales_list.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
a
=
file
.
readlines
()
#一次读取所有行并返回列表数据类型
print
(
a
)
final_sum
=
[]
for
i
in
a
:
data
=
i
.
split
()
#以空格分割列表i里的字符串(也就是用空格分割列表a的第一行)
sum
=
0
for
sales
in
data
[
1
:]:
#遍历每一个列表里所有的数字字符串
sum
=
sum
+
int
(
sales
)
#将每一个数字字符串转换为整型,然后加在sum里,即对每一行求和。
result
=
data
[
0
]
+
str
(
sum
)
+
'
\n
'
#将每一行的人名+求和的结果赋值给result
final_sum
.
append
(
result
)
with
open
(
r'C:\Users\10839\Desktop\sales_list.txt'
,
'a'
,
encoding
=
'utf-8'
)
as
file
:
file
.
writelines
(
final_sum
)
\ No newline at end of file
wjdx2.py
0 → 100644
View file @
8f10a005
with
open
(
r'c:\Users\10839\Desktop\义卖.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
a
=
file
.
readlines
()
final_sum
=
[]
for
i
in
a
:
sum
=
0
data
=
i
.
split
()
for
sales
in
data
[
1
:]:
sum
=
sum
+
int
(
sales
)
result
=
data
[
0
]
+
str
(
sum
)
+
'
\n
'
final_sum
.
append
(
result
)
with
open
(
r'c:\Users\10839\Desktop\义卖.txt'
,
'a'
,
encoding
=
'utf-8'
)
as
file
:
file
.
writelines
(
final_sum
)
\ No newline at end of file
文件读写 二.py
0 → 100644
View file @
8f10a005
with
open
(
r"C:\Users\10839\Desktop\sales_list.txt"
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
with
open
(
r"C:\Users\10839\Desktop\sales_list.txt"
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
#a=file.read()
#print(type(a)) #a为字符串类型
#print(type(a[3:5])) #取的是字符串里的数字12,也为字符串类型
a
=
file
.
readlines
()
#一次读取所有行并返回列表数据类型
#print(type(a)) #a的数据类型为列表
# print(type(a[3:5]))#数字12的数据类型也为列表
#print(a)
final_sum
=
[]
for
i
in
a
:
data
=
i
.
split
()
#以空格分割列表i里的字符串(也就是用空格分割列表a的第一行)
#print(data[0]) #打印data列表的第一个字符串,也就是姓名
#print(data)#打印姓名后的所有字符串(义卖款)
sum
=
0
for
sales
in
data
[
1
:]:
#遍历每一个列表里所有的数字字符串
sum
+=
int
(
sales
)
#将每一个数字字符串转换为整型,然后加在sum里,即对每一行求和。
result
=
data
[
0
]
+
str
(
sum
)
+
'
\n
'
#将每一行的人名+求和的结果赋值给result
final_sum
.
append
(
result
)
#print(result)
with
open
(
r"C:\Users\10839\Desktop\sales_list.txt"
,
'a'
,
encoding
=
'utf-8'
)
as
file
:
file
.
writelines
(
final_sum
)
\ 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