使用Python在文件中读取多个单词的字符串

问题描述 投票:1回答:2

我在用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', ...]

这也影响了年份列表,因为如果单词超过一个单词,它将从专业停止的地方开始。是否有人知道此问题已解决或我在做什么错?

python string list file
2个回答
0
投票

不要指望空格数。代替;根据列宽对线进行切片:

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())

-2
投票

快速而肮脏的解决方案是在双倍空格上分割:

row = count.split(' ')

然后您可以提取每个元素。另外,如注释中所述,您可以格式化文件以具有特定的分隔符

© www.soinside.com 2019 - 2024. All rights reserved.