Skip to content

Commit 54a106b

Browse files
committed
添加 csv 文件,导入对象数据
1 parent c845a09 commit 54a106b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

josephus.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
__author__ = 'Chongsen Zhao'
77

8+
from csv import reader
9+
810

911
class Person:
1012
"""Summary of class here.
@@ -82,15 +84,24 @@ def append(self, target):
8284
if __name__ == '__main__':
8385

8486
ring = RingSort()
85-
f_txt = open("people.txt")
86-
for line in f_txt:
87-
if line.count(',') != 1:
88-
raise IOError("格式错误")
89-
line = line.strip('\n')
90-
name = line.split(',')[0]
91-
age = line.split(',')[1]
87+
88+
f_csv = open("people.csv")
89+
data = reader(f_csv)
90+
for line in data:
91+
name = line[0]
92+
age = line[1]
9293
ring.append(Person(name, age))
93-
f_txt.close()
94+
f_csv.close()
95+
96+
# f_txt = open("people.txt")
97+
# for line in f_txt:
98+
# if line.count(',') != 1:
99+
# raise IOError("格式错误")
100+
# line = line.strip('\n')
101+
# name = line.split(',')[0]
102+
# age = line.split(',')[1]
103+
# ring.append(Person(name, age))
104+
# f_txt.close()
94105

95106
try:
96107
start = int(input("\nPlease input a starting position:"))

people.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"Jorn ",25
2+
"Tom ",30
3+
"Jerry ",35
4+
"Mike ",40

0 commit comments

Comments
 (0)