Question

Problem 1 (Reading Statistics) Write a function called read_stats(filename) that takes a csv (comma separated values)...

Problem 1 (Reading Statistics)

Write a function called read_stats(filename) that takes a csv (comma separated values) filename as an argument and returns a 2D list containing the statistics in that file. The returned list should contain lists of stats for each crime reported and should not include the header line from the file.

The file crime_in_vancouver.csv (included in the folder) has the following structure:

• The first row provides headers for each column

• Each subsequent row represents the stats for a single crime reported

• Each stat is separated by a comma (,)

• Each crime is described by the following stats:

1. TYPE: The type of crime activities

2. YEAR: A four-digit field that indicates the year when the reported crime activity occurred

3. MONTH: A numeric field that indicates the month when the reported crime activity occurred

4. DAY: A two-digit field that indicates the day of the month when the reported crime activity occurred

5. HOUR: A two-digit field that indicates the hour time (in 24 hours format) when the reported crime activity occurred 6. MINUTE: A two-digit field that indicates the minute when the reported crime activity occurred

7. BLOCK: Generalized location of the report crime activity

8. NEIGHBOURHOOD: Neighbourhoods within the City of Vancouver are based on the census tract (CT) concept within census metropolitan area (CMA).

9. X: Coordinate values are projected in UTM Zone

10. All data must be considered offset and users should not interpret any locations as related to a specific person or specific property. 10. Y: Coordinate values are projected in UTM Zone 10. All data must be considered offset and users should not interpret any locations as related to a specific person or specific property.

You may assume that the file is properly formatted, i.e., that the first column is the TYPE, the second is the YEAR and so on. See the sample output for an example.

Sample run:

read_stats('crime_in_vancouver.csv') returns [['Break and Enter Commercial', '2012', '12', '14', '8', '52', '', 'Oakridge', '491285', '5453433'], ['Break and Enter Commercial', '2019', '3', '7', '2', '6', '10XX SITKA SQ', 'Fairview', '490612.9648', '5457109.822'], ….]

In Python

Make sure the program is available. Please upload the complete program picture and program content.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import csv
def read_stats(filename):#method that returns 2D list
with open(filename) as f:#open file
reader=csv.reader(f)
data=list(reader)#read data to a list
return (data[1:])#return list

file=input("Enter file name:")
print(read_stats(file))

Screenshots:

The Screenshots are attached below for reference.

Please follow them for proper indentation.

The sample file with sample data is chosen for execution.

Add a comment
Know the answer?
Add Answer to:
Problem 1 (Reading Statistics) Write a function called read_stats(filename) that takes a csv (comma separated values)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function called filter_crime_stats(stats,param,x,y) that takes a 2D list of crime stats, a string argument...

    Write a function called filter_crime_stats(stats,param,x,y) that takes a 2D list of crime stats, a string argument param from the set {YEAR, MONTH, DAY}, an integer start value x and an integer end value y as arguments, and returns the 2D list of crime stats containing the crime type, year, month, day, and neighbourhood of all crimes reported between the start value and the end value of the {YEAR/MONTH/DAY}. The value of param is not case sensitive. For example, Year, YEAR,...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • Chapter overview 1. Reasons for international trade Resources reasons Economic reasons Other reasons 2. Difference between...

    Chapter overview 1. Reasons for international trade Resources reasons Economic reasons Other reasons 2. Difference between international trade and domestic trade More complex context More difficult and risky Higher management skills required 3. Basic concept s relating to international trade Visible trade & invisible trade Favorable trade & unfavorable trade General trade system & special trade system Volume of international trade & quantum of international trade Commodity composition of international trade Geographical composition of international trade Degree / ratio of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT