我在将一个以上单词的整个字符串存储到Python列表中时遇到问题。给定一个包含有关学生的信息的文件,例如名字,姓氏,专业及其年份,看起来像这样:
Terrence Jones Computer Science Freshman
Amy Johnson Biology Freshman
Craig Anderson Criminal Justice Sophomore
等等..
我的目标是创建一个将这些属性存储到列表中的程序。名字和姓氏有效,但是当我进入专业时,某些专业比其他专业更长,因此遇到了问题。这是我尝试使用的代码:
def main():
survey = open("survey.txt", "r")
lines = survey.readlines()
firstNames = [] # list that stores first names of students that filled out survey
lastNames = [] # list that stores last names of students that filled out survey
major = [] # list that stores the major of students that filled out survey
year = [] # list that stores the classification year of students that filled out survey
for count in lines:
# stores the information from file into the attributes for students
firstNames.append(count.split(' ')[0])
lastNames.append(count.split(' ')[1])
major.append(count.split()[2])
year.append(count.split()[3])
这是我打印专业列表时的输出:
['Computer', 'Biology', 'Criminal', ...]
我期待输出将显示
['Computer Science', 'Biology', 'Criminal Justice', ...]
这也影响了年份列表,因为如果单词不止一个单词,它将从专业停止的地方开始。有人碰巧知道解决此问题的方法或我做错了什么吗?
不要指望空格的数量。代替; 根据列宽对线进行切片:
0.................18..................38
Terrence Jones Computer Science Freshman
例如:
for line in lines:
full_name = line[:18].strip()
firstNames.append(full_name.split(" ")[0])
lastNames.append(full_name.split(" ")[1])
major.append(line[18:38].strip())
year.append(line[38:].strip())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句