africanxmamba
africanxmamba

Reputation: 113

Reading strings with more than word in a file using Python

I am having an issue storing an entire string with more than one word into a list in Python. Given a file that contains pieces of information about students such as first name, last name, major, and their year looks something like this:

Terrence Jones    Computer Science    Freshman
Amy Johnson       Biology             Freshman
Craig Anderson    Criminal Justice    Sophomore

And so on..

My goal is to create a program that stores these attributes into a list. First and last name works, but when I get to major where some majors are longer than others I run into an issue. This is the code that I have tried to use:

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

This is the output for when I print the majors list:

['Computer', 'Biology', 'Criminal', ...]

I was expecting an output that would display

['Computer Science', 'Biology', 'Criminal Justice', ...]

This is also affecting the year list because it picks up from where major stopped at if it was more than one word. Does anyone happen to know of a fix to this issue or what I am doing wrong?

Upvotes: 1

Views: 69

Answers (1)

Selcuk
Selcuk

Reputation: 59184

Do not count on the number of spaces. Instead; slice the line according to the column widths:

0.................18..................38
Terrence Jones    Computer Science    Freshman

For example:

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

Upvotes: 1

Related Questions