Week 2: Using Python to Interact with the Operating System Qwiklabs Assessment

First, you need to complete the prerequisites, Just copy-paste the code is given there.

Now, The First objective is Convert employee data to dictionary. The code is straightforward, you just have to copy-paste what’s given there.

#!/usr/bin/env python3
import csv
def read_employees(csv_file_location):
  csv.register_dialect('empDialect', skipinitialspace=True, strict=True)
  employee_file = csv.DictReader(open(csv_file_location), dialect = 'empDialect')
  employee_list = []
  for data in employee_file:
    employee_list.append(data)
  return employee_list

employee_list = read_employees('/home/give_your_username/data/employees.csv') #This here is <file_location>. You have to change the address and also change
print(employee_list)                                                               #the <username> into your given username


Note– Before you copy-paste the code from here or from the site make sure to change the <file-location> to /home/<username>/data/employees.csv and change <username> to your given username

employee_list = read_employees('<file_location>') #Don't forget to change this part in above code.
print(employee_list)

The third Objective is Process employee data. You will once again open generate_report.py file. You need to delete the print(employee_list). After this paste this code in the terminal.

def process_data(employee_list):
  department_list = []
  for employee_data in employee_list:
    department_list.append(employee_data['Department'])
  department_data = {}
  for department_name in set(department_list):
    department_data[department_name] = department_list.count(department_name)
  return department_data
dictionary = process_data(employee_list)
print(dictionary)

Now, The Final objective is Generate a report. Once again you will open the generate_report.py file. Like previously you need to delete print(dictionary) from the code. After this, you need to copy-paste the code but like previously you have to give the proper address for the report_file. Replace <report_file> to /home/<username>/data/report.txt , also replave <username> to your given username.

def write_report(dictionary, report_file):
  with open(report_file, "w+") as f:
    for k in sorted(dictionary):
      f.write(str(k)+':'+str(dictionary[k])+'\n')
    f.close()
write_report(dictionary, '/home/give_your_username/test_report.txt') # You have to replace <report_file> here.

The final code looks like this-

#!/usr/bin/env python3
import csv
def read_employees(csv_file_location):
  csv.register_dialect('empDialect', skipinitialspace=True, strict=True)
  employee_file = csv.DictReader(open(csv_file_location), dialect = 'empDialect')
  employee_list = []
  for data in employee_file:
    employee_list.append(data)
  return employee_list

employee_list = read_employees('/home/username_example-2323hkhk/data/employees.csv')


def process_data(employee_list):
  department_list = []
  for employee_data in employee_list:
    department_list.append(employee_data['Department'])
  department_data = {}
  for department_name in set(department_list):
    department_data[department_name] = department_list.count(department_name)
  return department_data
dictionary = process_data(employee_list)

def write_report(dictionary, report_file):
  with open(report_file, "w+") as f:
    for k in sorted(dictionary):
      f.write(str(k)+':'+str(dictionary[k])+'\n')
    f.close()
write_report(dictionary, '/home/username_example-2323hkhk/test_report.txt')


Leave a Comment