Python - 为 class 创建多个对象

Python - creating multiple objects for a class

在 python 中,我需要创建一个 class 'Student' 的 43 个实例,其中包括变量 first_name、middle_name、last_name , student_id 通过读取文件 (Students.txt) 并解析它。文本文件如下所示:

Last Name  Midle Name  First Name   Student ID  
----------------------------------------------
Howard                  Moe         howar1m     
Howard                  Curly       howar1c     
Fine                    Lary        fine1l      
Howard                  Shemp       howar1s     
Besser                  Joe         besse1j     
DeRita      Joe         Curly       derit1cj    
Tiure       Desilijic   Jaba        tiure1jd    
Tharen                  Bria        thare1b     
Tai         Besadii     Durga       tai1db      
Hego                    Damask      hego1d      
Lannister               Tyrion      lanni1t     
Stark                   Arya        stark1a     
Clegane                 Sandor      clega1s     
Targaryen               Daenerys    targa1d     
Bombadil                Tom         bomba1t     
Brandybuck              Meriadoc    brand1m     
Took                    Pregrin     took1p      
McCoy                   Leonard     mccoy1l     
Scott                   Montgomery  scott1m     
Crusher                 Wesley      crush1w     
Montoya                 Inigo       monto1i     
Rugen                   Tyrone      rugen1t     
Solo                    Han         solo1h      
Corey                   Carl        corey1c     
Flaumel                 Evelyn      flaum1e     
Taltos                  Vlad        talto1v     
e'Drien                 Morrolan    edrie1m     
Watson                  John        watso1j     
McCoy                   Ebenezar    mccoy1e     
Carpenter               Molly       carpe1m     
Graystone               Zoe         grays1z
Adama                   William     adama1w
Adama       Joseph      Leland      adama1l
Roslin                  Laura       rosli1l
Baltar                  Gaius       balta1g
Tigh                    Ellen       tigh1e
Tigh                    Saul        tigh1s
Cottle                  Sherman     cottl1s
Zarek                   Thomas      zarek1t
Murphy      James       Alexander   murph1a
Sobchak                 Walter      sobch1w
Dane                    Alexander   dane1a
Gruber                  Hans        grube1h
Biggs       John        Gil         biggs1gj

class 学生是:

class Student (object):
    def __init__(self, first_name, middle_name, last_name, student_id):
        self.__first_name = first_name
        self.__middle_name = middle_name
        self.__last_name = last_name
        self.__student_id = student_id

读入 'Students.txt' 并创建学生的每个实例的最简单方法是什么?

list_of_students = []
with open('students.txt') as f:
    for line in f:
        data = line.split()
        if len(data) == 3:
            firstname, lastname, id = data
            list_of_students.append(Student(firstname, '', lastname, id))
        elif len(data) == 4:
            list_of_students.append(Student(*data))
        else:
            raise ValueError

我不确定你的输入文件是如何布局的,所以这里有一些处理来处理没有中间名的情况。

分步教程

要读取文件内容,请使用io.open。如果任何名称有重音字符,请不要忘记指定文件编码。

with io.open('students.txt', mode="r", encoding="utf8") as fd:
    content = fd.read()

这里,你读取了全部内容,存入内存(数据量小)。您也可以使用迭代器。

然后,您可以将内容逐行拆分 str.splitlines():

lines = content.splitlines()
# print(lines)

你会得到类似的东西:

['Last Name  Midle Name  First Name   Student ID  ',
 '----------------------------------------------',
 'Howard                  Moe         howar1m     ',
 'Howard                  Curly       howar1c     ',
 'Fine                    Lary        fine1l      ',
 'Howard                  Shemp       howar1s     ',
 'Besser                  Joe         besse1j     ',
 'DeRita      Joe         Curly       derit1cj    ',
 'Tiure       Desilijic   Jaba        tiure1jd    ',
 'Tharen                  Bria        thare1b     ']

您有(将近)fixed-length 行,因此您可以使用切片来提取字段。

以下是您可以为header做的事情:

header = lines.pop(0)
fields = header[0:8], header[11:21], header[23:33], header[36:46]
# print(fields)

你得到:

('Last Nam', 'Midle Name', 'First Name', 'Student ID')

您可以删除连字符行:

lines.pop(0)

对于每一行,您也可以使用切片提取值。注意:切片索引略有不同:

for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    # print(record)

您将获得尾随 space:

的值
('Howard  ', '         ', ' Moe       ', 'howar1m   ')
('Howard  ', '         ', ' Curly     ', 'howar1c   ')
('Fine    ', '         ', ' Lary      ', 'fine1l    ')
('Howard  ', '         ', ' Shemp     ', 'howar1s   ')
('Besser  ', '         ', ' Joe       ', 'besse1j   ')
('DeRita  ', 'Joe      ', ' Curly     ', 'derit1cj  ')
('Tiure   ', 'Desilijic', ' Jaba      ', 'tiure1jd  ')
('Tharen  ', '         ', ' Bria      ', 'thare1b   ')

要避免尾随 spaces,请使用 str.strip() 函数:

for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    record = [v.strip() for v in record]
    # print(record)

你得到:

['Howard', '', 'Moe', 'howar1m']
['Howard', '', 'Curly', 'howar1c']
['Fine', '', 'Lary', 'fine1l']
['Howard', '', 'Shemp', 'howar1s']
['Besser', '', 'Joe', 'besse1j']
['DeRita', 'Joe', 'Curly', 'derit1cj']
['Tiure', 'Desilijic', 'Jaba', 'tiure1jd']
['Tharen', '', 'Bria', 'thare1b']

此时,我建议您将记录存储为列表中的 dict

records = []
for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    record = [v.strip() for v in record]
    records.append(dict(zip(header, record)))

你得到:

[{'First Name': 'Moe', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1m'},
 {'First Name': 'Curly', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1c'},
 {'First Name': 'Lary', 'Last Nam': 'Fine', 'Midle Name': '', 'Student ID': 'fine1l'},
 {'First Name': 'Shemp', 'Last Nam': 'Howard', 'Midle Name': '', 'Student ID': 'howar1s'},
 {'First Name': 'Joe', 'Last Nam': 'Besser', 'Midle Name': '', 'Student ID': 'besse1j'},
 {'First Name': 'Curly', 'Last Nam': 'DeRita', 'Midle Name': 'Joe', 'Student ID': 'derit1cj'},
 {'First Name': 'Jaba', 'Last Nam': 'Tiure', 'Midle Name': 'Desilijic', 'Student ID': 'tiure1jd'},
 {'First Name': 'Bria', 'Last Nam': 'Tharen', 'Midle Name': '', 'Student ID': 'thare1b'}]

但你也可以使用 class:

class Student(object):
    def __init__(self, first_name, middle_name, last_name, student_id):
        self.first_name = first_name
        self.middle_name = middle_name
        self.last_name = last_name
        self.student_id = student_id
    
    def __repr__(self):
        fmt = "<Student('{first_name}', '{middle_name}', '{last_name}', '{student_id}')>"
        return fmt.format(first_name=self.first_name, middle_name=self.middle_name, last_name=self.last_name, student_id=self.student_id)
        

并构建学生列表:

students = []
for line in lines:
    record = line[0:8], line[12:21], line[23:34], line[36:46]
    record = [v.strip() for v in record]
    students.append(Student(*record))

你得到:

[<Student('Howard', '', 'Moe', 'howar1m')>,
 <Student('Howard', '', 'Curly', 'howar1c')>,
 <Student('Fine', '', 'Lary', 'fine1l')>,
 <Student('Howard', '', 'Shemp', 'howar1s')>,
 <Student('Besser', '', 'Joe', 'besse1j')>,
 <Student('DeRita', 'Joe', 'Curly', 'derit1cj')>,
 <Student('Tiure', 'Desilijic', 'Jaba', 'tiure1jd')>,
 <Student('Tharen', '', 'Bria', 'thare1b')>]