This commit is contained in:
Yavuz Sava 2024-11-13 15:17:51 +03:00
parent 6f01dfbb7f
commit bc35e7503c

View File

@ -367,6 +367,100 @@
"#Call the function\n",
"print(contents_of_file(\"flowers.csv\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Qwiklabs - Handle Files\n",
"# cd data\n",
"# ls\n",
"# cat employees.csv\n",
"\n",
"''' input:\n",
"Full Name, Username, Department\n",
"Audrey Miller, audrey, Development\n",
"Arden Garcia, ardeng, Sales\n",
"Bailey Thomas, baileyt, Human Resources\n",
"Blake Sousa, sousa, IT infrastructure\n",
"Cameron Nguyen, nguyen, Marketing\n",
"Charlie Grey, greyc, Development\n",
"Chris Black, chrisb, User Experience Research\n",
"Courtney Silva, silva, IT infrastructure\n",
"Darcy Johnsonn, darcy, IT infrastructure\n",
"Elliot Lamb, elliotl, Development\n",
"Emery Halls, halls, Sales\n",
"Flynn McMillan, flynn, Marketing\n",
"Harley Klose, harley, Human Resources\n",
"Jean May Coy, jeanm, Vendor operations\n",
"Kay Stevens, kstev, Sales\n",
"Lio Nelson, lion, User Experience Research\n",
"Logan Tillas, tillas, Vendor operations\n",
"Micah Lopes, micah, Development\n",
"Sol Mansi, solm, IT infrastructure\n",
"'''\n",
"\n",
"#!/usr/bin/env python3\n",
"import csv\n",
"\n",
"def read_employees(csv_file_location):\n",
" # Dialect classes can be registered by name so that callers of the CSV module\n",
" # don't need to know the parameter settings in advance. We will now register a\n",
" # dialect empDialect.\n",
" csv.register_dialect('empDialect', skipinitialspace=True, strict=True)\n",
" # Append the dictionaries to an empty initialised list employee_list as you\n",
" # iterate over the CSV file.\n",
" employee_file = csv.DictReader(open(csv_file_location), dialect = 'empDialect')\n",
" employee_list = []\n",
" for data in employee_file:\n",
" employee_list.append(dict(data))\n",
" return employee_list\n",
"\n",
"def process_data(employee_list):\n",
" # Now, initialize a new list called department_list, iterate over employee_list,\n",
" # and add only the departments into the department_list.\n",
" department_list = []\n",
" for employee_data in employee_list:\n",
" department_list.append(employee_data['Department'])\n",
" # The department_list should now have a redundant list of all the department\n",
" # names. We now have to remove the redundancy and return a dictionary. We will\n",
" # return this dictionary in the format department:amount, where amount is the\n",
" # number of employees in that particular department.\n",
" department_data = {}\n",
" for department_name in set(department_list):\n",
" department_data[department_name] = department_list.count(department_name)\n",
" return department_data\n",
"\n",
"def write_report(dictionary, report_file):\n",
" with open(report_file, \"w+\") as f:\n",
" for k in sorted(dictionary):\n",
" f.write(str(k) + ':' + str(dictionary[k]) + '\\n')\n",
" f.close()\n",
"\n",
"employee_list = read_employees('/home/student/data/employees.csv')\n",
"dictionary = process_data(employee_list)\n",
"write_report(dictionary, '/home/student/data/report.txt')\n",
"\n",
"''' output1:\n",
"[{'Full Name': 'Audrey Miller', 'Username': 'audrey', 'Department': 'Development'}, {'Full Name': 'Arden Garcia', 'Username': 'ardeng', 'Department': 'Sales'}, {'Full Name': 'Bailey Thomas', 'Username': 'baileyt', 'Department': 'Human Resources'}, {'Full Name': 'Blake Sousa', 'Username': 'sousa', 'Department': 'IT infrastructure'}, {'Full Name': 'Cameron Nguyen', 'Username': 'nguyen', 'Department': 'Marketing'}, {'Full Name': 'Charlie Grey', 'Username': 'greyc', 'Department': 'Development'}, {'Full Name': 'Chris Black', 'Username': 'chrisb', 'Department': 'User Experience Research'}, {'Full Name': 'Courtney Silva', 'Username': 'silva', 'Department': 'IT infrastructure'}, {'Full Name': 'Darcy Johnsonn', 'Username': 'darcy', 'Department': 'IT infrastructure'}, {'Full Name': 'Elliot Lamb', 'Username': 'elliotl', 'Department': 'Development'}, {'Full Name': 'Emery Halls', 'Username': 'halls', 'Department': 'Sales'}, {'Full Name': 'Flynn McMillan', 'Username': 'flynn', 'Department': 'Marketing'}, {'Full Name': 'Harley Klose', 'Username': 'harley', 'Department': 'Human Resources'}, {'Full Name': 'Jean May Coy', 'Username': 'jeanm', 'Department': 'Vendor operations'}, {'Full Name': 'Kay Stevens', 'Username': 'kstev', 'Department': 'Sales'}, {'Full Name': 'Lio Nelson', 'Username': 'lion', 'Department': 'User Experience Research'}, {'Full Name': 'Logan Tillas', 'Username': 'tillas', 'Department': 'Vendor operations'}, {'Full Name': 'Micah Lopes', 'Username': 'micah', 'Department': 'Development'}, {'Full Name': 'Sol Mansi', 'Username': 'solm', 'Department': 'IT infrastructure'}]\n",
"'''\n",
"\n",
"'''output2:\n",
"{'Sales': 3, 'Human Resources': 2, 'Development': 4, 'Marketing': 2, 'User Experience Research': 2, 'Vendor operations': 2, 'IT infrastructure': 4}\n",
"'''\n",
"\n",
"'''output3:\n",
"Development:4\n",
"Human Resources:2\n",
"IT infrastructure:4\n",
"Marketing:2\n",
"Sales:3\n",
"User Experience Research:2\n",
"Vendor operations:2\n",
"'''"
]
}
],
"metadata": {