Skip to content

Commit 1567441

Browse files
committed
使用with语句打开文件
1 parent 9e92f54 commit 1567441

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

josephus.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,29 @@ def append(self, target):
8787

8888
ring = RingSort()
8989

90-
f_zip = ZipFile('people.zip')
91-
f_csv = TextIOWrapper(f_zip.open('people.csv'))
92-
data = reader(f_csv)
93-
for line in data:
94-
name = line[0]
95-
age = line[1]
96-
ring.append(Person(name, age))
97-
f_csv.close()
98-
f_zip.close()
99-
100-
# f_csv = open("people.csv")
101-
# data = reader(f_csv)
102-
# for line in data:
103-
# name = line[0]
104-
# age = line[1]
105-
# ring.append(Person(name, age))
106-
# f_csv.close()
107-
108-
# f_txt = open("people.txt")
109-
# for line in f_txt:
110-
# if line.count(',') != 1:
111-
# raise IOError("格式错误")
112-
# line = line.strip('\n')
113-
# name = line.split(',')[0]
114-
# age = line.split(',')[1]
115-
# ring.append(Person(name, age))
116-
# f_txt.close()
90+
with ZipFile('people.zip') as f_zip:
91+
with TextIOWrapper(f_zip.open('people.csv')) as f_csv:
92+
data = reader(f_csv)
93+
for line in data:
94+
name = line[0]
95+
age = line[1]
96+
ring.append(Person(name, age))
97+
98+
# with open("people.csv") as f_csv:
99+
# data = reader(f_csv)
100+
# for line in data:
101+
# name = line[0]
102+
# age = line[1]
103+
# ring.append(Person(name, age))
104+
105+
# with open("people.txt") as f_txt:
106+
# for line in f_txt:
107+
# if line.count(',') != 1:
108+
# raise IOError("格式错误")
109+
# line = line.strip('\n')
110+
# name = line.split(',')[0]
111+
# age = line.split(',')[1]
112+
# ring.append(Person(name, age))
117113

118114
try:
119115
start = int(input("\nPlease input a starting position:"))

0 commit comments

Comments
 (0)