Earthling Person
Earthling Person

Reputation: 19

Converting File Data to Lists

I have a file on sample employee data. The first line is the name, the second line is the salary, the third line is life insurance election (Y/N), the fourth line is health insurance election (PPOI, PPOF, None), and it repeats. A snippet of the file is below:

Joffrey Baratheon
190922
Y
PPOI
Gregor Clegane
47226
Y
PPOI
Khal Drogo
133594
N
PPOI
Hodor
162581
Y
PPOF
Cersei Lannister
163985
N
PPOI
Tyrion Lannister
109253
N
PPOF
Jorah Mormont
61078
Y
None
Jon Snow
123222
N
None

How can I take this file data and extract each data type (name, salary, life insurance, health insurance) into four separate lists?
Currently, my code is creating a multidimensional list by employee, but I ultimately want four separate lists. My current code is below:

def fileread(text):
    in_file = open(text, "r")
    permlist = []
    x = 1
    templist = []
    for line in in_file:
        line = line.strip()
        templist.append(line)
        if x == 4:
            permlist.append(templist)
            templist = []
            x = 1
        else:
            x+=1
    return (permlist)
def main ():
    EmpData = fileread("EmployeeData.txt")
    index = 0
    print (EmpData[index])

Upvotes: 0

Views: 129

Answers (3)

Alex
Alex

Reputation: 21766

You can use islice from the itertools library. It will allow you to iterate over batches of 4 lines at a time.

from itertools import islice
EmpData = []
headers = ['name', 'salary', 'life insurance', 'health insurance']
record = {}
counter = 1
with open('data.txt', 'r') as infile:
    while counter>0:
        lines_gen = islice(infile, 4)
        counter = 0
        hasLines = False;
        for line in lines_gen:
            record[headers[counter]] = line.strip()
            counter += 1
        EmpData.append(record)
index = 0
print (EmpData[index])

As someone has complained about scholastic dishonesty violations in this post, I would make to clear that is a simplified version of a production code snippet that was inspired by this SO answer: How to read file N lines at a time in Python?.

Upvotes: 1

coder
coder

Reputation: 12992

You could use 4 list comprehensions like so:

with open("file.txt",'r') as f:
    lines = f.readlines()

name_list = [lines[i].rstrip() for i in range(0,len(lines),4)]
salary_list = [lines[i].rstrip() for i in range(1,len(lines),4)]
life_ins_list = [lines[i].rstrip() for i in range(2,len(lines),4)]
health_ins_list = [lines[i].rstrip() for i in range(3,len(lines),4)]

Upvotes: 2

B3Caballero
B3Caballero

Reputation: 78

Count the total lines, divide by four get number of people to add to the lists.

i = 0
while i < num_of_people:
    for a in range(0, num_of_people+1):
        namelist.append(i)
        i += 1
        salarylist.append(i)
        i +=1
        ...

Be careful splitting the data like this. Its easy to get confused. You might be better off storing this data into a database.

Upvotes: 1

Related Questions