Question: Some examples of importing the script and calling GetDataByHeaderAndRow are below. C: Users brokow UCM C 1 asses ME 0 2

Some examples of importing the script and calling GetDataByHeaderAndRow are below.
C:\Users\brokow\UCM C1asses\ME 021\2024 Fa11 Assignments> pythonHW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'LABEL', 3)
'Treasure'HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'Name', 5)
'66493''66494'Warning: header Python File Reading and Dictionaries: More Practice
Storing data and adding to data in a dictionary: 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
(2in 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.).
Data stored in a dictionary for easy lookup
The layout of the data in a given file type 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 integer data row number, increasing by 1
for each row, starting at 0. The remaining column data are strings, even if some look like numbers. An example
(available on CatCourses as HW05_01_data_1.txt) is below.
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.
Other sample 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 called exactly HW05_01. py that has a function called GetDataDict that takes a data file name as
a string and reads the data file with that file name. The file will be in the same folder as your Python script. As
always, do this using built-in Python functions, not using any imported data reading modules. The function will
return a dictionary with the headers as keys and the data for the corresponding columns in lists as the keys'
values. The data for the first column of row number data does not need to be stored. The values can remain as
strings. An example of importing the script and calling GetDatadict is below.
C:\Users\brokow\UCM Classes\ME 021\2024 Fa11 Assignments> pythonHW05_01.GetDataDict('Hw05_01_data_1.txt')
{'LABEL': ['default', 'Potion', 'Scroll', 'Treasure', 'Body', 'Bones', 'Pouch'], 'Name': ['6671',
'66483','66484','66490','66492','66493','66494'], 'Appearance': ['****','194','195',
'196','198','199','200']}
>>>
The same script will have a second function called GetDataByHeaderAndRow that will take three arguments:
the data file name as a string, the header name as a string, and a row number as an integer (starting at 0). It will
return a string which is the file's data for the given column header and row number. Obviously, it should make
use of GetDataDict. This function must do some checking on the last two parameters given to it:
If the given header is not one of the headers for the data, it will print an informative warning to that
effect and return None.
If the given row is outside the range of rows for the data, it will print an informative warning to that
effect and return None.
Note that neither function should make any assumptions about the number of headers or rows. Both functions
should work without adjustment for other files with the same data layout.
Some examples of importing the script and calling GetDataByHeaderAndRow are below.
C:\Users\brokow\UCM Classes\ME 021\2024 Fall Assignments> python
>>> import HW05_01
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'LABEL', 3)
'Treasure'
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'Appearance', 0)
'****'
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'Name', 5)
'66493'
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'Name', 6)
'66494'
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'LoooBEL', 2)
Warning: header was not in the data read from file "HW05_01_data_1.txt". Quitting.
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_1.txt', 'LABEL', 16)
Warning: file "HW05_01_data_1.txt" has row numbers going from 0 to 6. Quitting.
>>> # try a couple of the other files
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_3.txt', 'MDLNAME', 9)
'NECK'
>>> HW05_01.GetDataByHeaderAndRow('HW05_01_data_4.txt', 'LABEL', 6)
'Fancy'
>>>
Some examples of importing the script and calling

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!