The language is Python 3.6 Write a function parse_data (filename) that reads in
ID: 3885513 • Letter: T
Question
The language is Python 3.6
Write a function parse_data (filename) that reads in a series of records from a file, each corresponding to a reported case of flu (from the years 2010-2014), and converts the data into a collection of monthly case counts for each year. The function should return a 3-tuple containing a nested list of monthly case counts for each year. This nested list should be 5 x 12 elements in size (please see below in the example); the number of valid records; and the number of invalid records The data in the file consists of records. Each record consists of four items: year, month (3-character code), age and sex. You will only need to use the first two items (year and month) This is an example of the data in the file case list.csv year,month,age,sex 2011,APR, 29,M 2011,MAR,37,F 2012,MAY,63,M 2012,Mxx,63,M Assumptions You can assume that each record is correctly formatted, but your function should only count valid records. A valid record is a record that has valid values for year and month. Valid years are: 2010, 2011,2012,2013,2014 Valid months are: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC Invalid records should be ignored >>> raw-data parse-data( 'case-list.csv ') =Explanation / Answer
We will then use Python's open() function to open our days.txt file. The open() function requires as its first argument the file path. The function also allows for many other parameters. However, most important is the optional mode parameter. Mode is an optional string that specifies the mode in which the file is opened. The mode you choose will depend on what you wish to do with the file. Here are some of our mode options:
In this example, we only want to read from the file, so we will use the 'r' mode. We will use the open() function to open the days.txt file and assign it to the variable days_file.
files.py
After we have opened the file, we can then read from it, which we will do in the next step.
Step 3 — Reading a File
Since our file has been opened, we can now manipulate it (i.e. read from it) through the variable we assigned to it. Python provides three related operations for reading information from a file. We'll show how to use all three operations as examples that you can try out to get an understanding of how they work.
The first operation <file>.read() returns the entire contents of the file as a single string.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.