Blake Armstrong
Blake Armstrong

Reputation: 1

I need some help understanding what is happening here?

Screenshot of the CSV file I am inputting

This is my code that I am trying to run. I am trying to make call functions to make the program run. get_toc_tuple is supposed to split the lines in columns. read_toc is supposed to read the file and copy the columns to a list. get_cell is one of the options you would be able to do. Eventually I want to be able to get_work_phone and get_shift. print_cell is supposed to print the correct info for name and cell phone number.
I can't figure out why these call functions are not working.

It is only printing:

Team Member      Cell Phone

My Code:

import re



def get_toc_tuple(line_str):
    data = re.split(r'\s{2,}', line_str)
    
    team_member = data[0]
    desk_phone = data[2]
    cell_phone = data[3]
    shift = data[4]
    return (team_member, desk_phone, cell_phone, shift)


def read_toc(filename):
    fileread = open(filename, 'r')
    toc_list = []
    fileread.readlines()[0]
    for line in fileread.readlines()[1:]:
            toc_list.append(get_toc_tuple(line.strip()))
    
    fileread.close()
    return toc_list


def get_cell(toc_list):
    cell_list = []
    for tup in toc_list:
        if name in tup[0]:
            cell_list.append(tup[3])
    return cell_list


def print_cell(cell_list):
    print('%-19s%-1s%-13s\n' % ('Team Name', ' ', 'Cell Phone'))
    for line in cell_list:
        team_member = line[0]
        cell_phone = line[3]
        print('%-19s%-1s%-13s\n' % (team_member, ' ', cell_phone))
       
if __name__ == '__main__':
    option = input("Cell Phone, Work Phone, or Shift? ")
    name = input('Employee: ')
    filename = input('Select a file location: ')
    toc_list = read_toc(filename)   
    if option == 'Cell Phone':
        cell_list = get_cell(toc_list)
        print_cell(cell_list)


  [1]: https://i.sstatic.net/ZrblW.png

Upvotes: 0

Views: 56

Answers (1)

Dan Constantinescu
Dan Constantinescu

Reputation: 1541

These kind of problems are easiest to solve using the csv module. Try to export your file in csv format (using excel or any other tool):

Team Member,Title,Desk Phone,Cell Phone,Shift
Bob Smith,Technical Support,xxx-xxx-xxxx,xxx-xxx-xxx,M-F 8a-5p
Todd Wilson,Technical Support,616-233-5065,734-709-1472,M-F 8a-5p

Then use:

import csv

rows = []
file = open("D:/team.csv")
with file:
    read = csv.DictReader(file)
    for row in read:
        rows.append(dict(row))

After above code is executed, rows list will contain the rows in the file mapped on the header row items:

[{'Team Member': 'Bob Smith', 'Title': 'Technical Support', 'Desk Phone': 'xxx-xxx-xxxx', 'Cell Phone': 'xxx-xxx-xxx', 'Shift': 'M-F 8a-5p'}, {'Team Member': 'Todd Wilson', 'Title': 'Technical Support', 'Desk Phone': '616-233-5065', 'Cell Phone': '734-709-1472', 'Shift': 'M-F 8a-5p'}]

Once you have this list is very easy to parse it and extract any info you want. For example Desk phone and Shift for Team Member Todd Wilson, will be something like:

for row in rows:
    if row['Team Member'] == 'Todd Wilson':
        print(row['Desk Phone'], row['Shift'])

Upvotes: 1

Related Questions