Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Experiment with Python dictionaries and their keys () member function that returns a list of a dictionary's keys. You can check if a particular
Experiment with Python dictionaries and their keys () member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict. This can be used in conditional statements (if statements, while statements, etc.). 1. Data stored in a dictionary for easy lookup Some data files exists such that the layout of the data in them is consistent. The files have a first row of headers and the remaining rows of data. The first column of data has no header, but is always the data row number, increasing by 1 for each row, starting at O. An example (available on CatCourses as HW05_01_data_1.txt) is below. 0 1 2 3456 6 LABEL default Potion Scroll Treasure Body Bones Pouch Name 6671 66483 66484 66490 66492 66493 66494 Appearance **** 194 195 196 198 199 200 Look over the data file. You will notice that the columns are separated by spaces and no data or header has a space inside. There will be other data files, but they will follow the same format, even if they have different headers, numbers of columns, numbers of rows, and spacing between columns. Some other data files with the same layout are given in CatCourses as HW05_01_data_2.txt, HW05_01_data 3. txt, and HW05_01_data_4.txt. Write a script that has a function that is given a data file name as a string and reads such a data file. As always, do this using built-in Python function, not using any imported data reading modules. The function will return a dictionary with the headers as keys and the data for the corresponding column in a list as that key's values. The data for the first column of row number data does not need to be stored. That values can remain as strings. The function should be called GetDataDict. An example of its use in PythonWin is below. >>> import os >>> os.chdir (r'c:\Users\brokow\UCM Classes\ME 021\2023 Spring Assignments') >>> HW05_01. GetDataDict('HW05_01_data_1.txt') {"LABEL": ['default', 'Potion', 'scroll', 'Treasure', 'Body', 'Bones', 'Pouch'], 'Name': ['6671', 66483', '66484', '66490', '66492', '66493', '66494'1, 'Appearance': ['****', '194', '195, '196', '198', '199', '200]} >>>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
import collections dicton ny whilenn ifn0 break else k rawinputenter name i...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started