<h2>分步教程</h2>
<p>要读取文件内容,请使用<code>io.open</code>。如果任何名称有强调字符,请不要忘记指定文件编码。在</p>
<pre><code>with io.open('students.txt', mode="r", encoding="utf8") as fd:
content = fd.read()
</code></pre>
<p>在这里,您读取整个内容并将其存储在内存中(数据量很小)。也可以使用迭代器。在</p>
<p>然后,可以使用<code>str.splitlines()</code>逐行拆分内容:</p>
^{pr2}$
<p>你会得到这样的信息:</p>
<pre><code>['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 ']
</code></pre>
<p>您有(几乎)固定长度的线,所以您可以使用切片来提取字段。在</p>
<p>以下是您可以对标题执行的操作:</p>
<pre><code>header = lines.pop(0)
fields = header[0:8], header[11:21], header[23:33], header[36:46]
# print(fields)
</code></pre>
<p>你会得到:</p>
<pre><code>('Last Nam', 'Midle Name', 'First Name', 'Student ID')
</code></pre>
<p>您可以删除连字符行:</p>
<pre><code>lines.pop(0)
</code></pre>
<p>对于每一行,也可以使用切片提取值。注:切片指数略有不同:</p>
<pre><code>for line in lines:
record = line[0:8], line[12:21], line[23:34], line[36:46]
# print(record)
</code></pre>
<p>您将得到带有尾随空格的值:</p>
<pre><code>('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 ')
</code></pre>
<p>若要避免尾随空格,请使用<code>str.strip()</code>函数:</p>
<pre><code>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)
</code></pre>
<p>你会得到:</p>
<pre><code>['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']
</code></pre>
<p>此时,我建议您将记录存储为列表中的<code>dict</code>:</p>
<pre><code>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)))
</code></pre>
<p>你会得到:</p>
<pre><code>[{'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'}]
</code></pre>
<p>但您也可以使用一个类:</p>
<pre><code>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)
</code></pre>
<p>建立学生名单:</p>
<pre><code>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))
</code></pre>
<p>你会得到:</p>
<pre><code>[<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')>]
</code></pre>