Reputation: 59
I have a text file that lists the first and last names of a set of employees, their ID numbers, genders and years of experience. The first line contains the number of employees. It's called employees.txt. Here it is:
7
John
Doe
33
272838
M
Mary
Johnson
38
3849383
F
Opie
Dog
6
839293
M
Missy
Dog
4
238392
F
John
Bla
28
834282
M
Amelia
Spien
2
5789812
F
Shane
CasenStien
2
567891
M
I want to organize this information into a list of dictionaries and then find all the female employees and print out their first and last names. I can get the list of dictionaries to work, but I'm getting an error when I try to find and print out the names of the female employees. Here's my code:
def employee_write(file):
employee_dict = {}
employee_dict["First"] = file.readline().strip()
employee_dict["Last"] = file.readline().strip()
employee_dict["Experience"] = file.readline().strip()
employee_dict["ID"] = file.readline().strip()
employee_dict["Gender"] = file.readline().strip()
return employee_dict
def female_print(x2):
for term in x2:
for word in employee_dict:
if employee_dict["Gender"] == "F":
print(employee_dict["First"])
print(employee_dict["Last"])
def main():
file = open("employees.txt", "r")
n = int(file.readline())
x1 = employee_write(file)
employee_list = []
for i in range(n):
employee_list.append(x1)
x2 = employee_list
print(x2)
female_print(x2)
main()
What is my error here? The female_print() function is not working because it says that the name 'employee_dict' is not defined. What's the problem?
Upvotes: 0
Views: 189
Reputation: 43495
In main
, the function employee_write
, (which should probably be called employee_read
instead) is only called once.
Try something like this instead:
In [1]: with open('employees.txt', 'r') as ef:
txt = ef.read()
...:
In [2]: lines = txt.split()
In [3]: records = [lines[i:i+5] for i in xrange(1, len(lines), 5)]
In [4]: records
Out[4]: [['John', 'Doe', '33', '272838', 'M'], ['Mary', 'Johnson', '38', '3849383', 'F'], ['Opie', 'Dog', '6', '839293', 'M'], ['Missy', 'Dog', '4', '238392', 'F'], ['John', 'Bla', '28', '834282', 'M'], ['Amelia', 'Spien', '2', '5789812', 'F'], ['Shane', 'CasenStien', '2', '567891', 'M']]
In []: for r in records:
print r
....:
['John', 'Doe', '33', '272838', 'M']
['Mary', 'Johnson', '38', '3849383', 'F']
['Opie', 'Dog', '6', '839293', 'M']
['Missy', 'Dog', '4', '238392', 'F']
['John', 'Bla', '28', '834282', 'M']
['Amelia', 'Spien', '2', '5789812', 'F']
['Shane', 'CasenStien', '2', '567891', 'M']
N.B.: this code assumes that the input file is formatted correctly!
Upvotes: 0
Reputation: 18917
I have some trouble to understand your variables naming and usage, so I've simplified it a little:
def employee_write(file):
employee_dict = {}
employee_dict["First"] = file.readline().strip()
employee_dict["Last"] = file.readline().strip()
employee_dict["Experience"] = file.readline().strip()
employee_dict["ID"] = file.readline().strip()
employee_dict["Gender"] = file.readline().strip()
return employee_dict
def female_print(employee_list):
for employee in employee_list:
if employee["Gender"] == "F":
print(employee["First"])
print(employee["Last"])
def main():
file = open("employees.txt", "r")
n = int(file.readline())
employee_list = []
for i in range(n):
employee_list.append(employee_write(file))
print(employee_list)
female_print(employee_list)
main()
which yields
[{'Gender': 'M', 'Last': 'Doe', 'ID': '272838', 'Experience': '33', 'First': 'John'}, {'Gender': 'F', 'Last': 'Johnson', 'ID': '3849383', 'Experience': '38', 'First': 'Mary'}, {'Gender': 'M', 'Last': 'Dog', 'ID': '839293', 'Experience': '6', 'First': 'Opie'}, {'Gender': 'F', 'Last': 'Dog', 'ID': '238392', 'Experience': '4', 'First': 'Missy'}, {'Gender': 'M', 'Last': 'Bla', 'ID': '834282', 'Experience': '28', 'First': 'John'}, {'Gender': 'F', 'Last': 'Spien', 'ID': '5789812', 'Experience': '2', 'First': 'Amelia'}, {'Gender': 'M', 'Last': 'CasenStien', 'ID': '567891', 'Experience': '2', 'First': 'Shane'}]
Mary
Johnson
Missy
Dog
Amelia
Spin
Is that what you want?
Upvotes: 1
Reputation: 10903
You have two (major) issues here. For one, you're not actually reading in the whole file. You're reading in the first record (as x1) then appending that record to a list N times. You can see from the print that you're only getting one record.
Also, your print_female function is referencing a non-existant variable "employee_dict" - you probably need to define that somewhere. Perhaps you meant to use "term"? This throws a stack trace.
If you fix these two issues, the basic premise of your code is sound.
Upvotes: 0