Knowee
Questions
Features
Study Tools

Write a function max_city_temp(csv_filename, city) which analyses temperatures recorded in a CSV file, and returns the maximum temperature recorded for the named city.Once again, the first column of the CSV file will be the city name, and the rest of the columns will be months of the year. The first row of the CSV file will provide the column headings. Here is an example file fragment:max_temp_small.csvcity/month,Jan,Feb,Mar,AprMelbourne,41.2,35.5,37.4,29.3Brisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Here is an example of how max_city_temp() should work:>>> max_city_temp('max_temp_small.csv', 'Brisbane')40.2

Question

Write a function max_city_temp(csv_filename, city) which analyses temperatures recorded in a CSV file, and returns the maximum temperature recorded for the named city.Once again, the first column of the CSV file will be the city name, and the rest of the columns will be months of the year. The first row of the CSV file will provide the column headings. Here is an example file fragment:max_temp_small.csvcity/month,Jan,Feb,Mar,AprMelbourne,41.2,35.5,37.4,29.3Brisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Here is an example of how max_city_temp() should work:>>> max_city_temp('max_temp_small.csv', 'Brisbane')40.2

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python function that does what you asked for:

import csv

def max_city_temp(csv_filename, city):
    with open(csv_filename, 'r') as file:
        reader = csv.reader(file)
        header = next(reader)
        for row in reader:
            if row[0] == city:
                return max(map(float, row[1:]))

print(max_city_temp('max_temp_small.csv', 'Brisbane'))

This function works as follows:

  1. It opens the CSV file in read mode.
  2. It creates a CSV reader object to read the file.
  3. It reads the first row of the file (the header) and ignores it.
  4. It iterates over the remaining rows in the file.
  5. If the city name in the first column of a row matches the city parameter, it converts the rest of the row to floats and returns the maximum value.

The max() function is used to find the maximum value in a list. The map() function is used to apply the float() function to each element in the list of temperatures.

This problem has been solved

Similar Questions

Write a function sort_records(csv_filename, new_filename) that sorts the records of a CSV file and writes the results as a new CSV file. The first column of the CSV file will be the city name. The rest of the columns will be months of the year. The first row of the CSV file will take the form of the column headings, with all columns other than the first being months of the year. Here is an example file fragment:max_temp.csvcity/month,Jan,Feb,Mar,AprMelbourne,41.2,35.5,37.4,29.3Brisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Note that your code will be tested over different CSV files, with different ranges of months in them. Irrespective of the exact months contained in the file, you may assume that the city name will always be in the first column, and the months in the remaining columns.You must sort the data in alphabetical order according to the city name (stored in the first column). Your program should write the sorted records to a new file with the name given by the argument new_filename.Here is an example of how sort_records() should work. 'program.py' is the program and below is its terminal output.sort_records('max_temp.csv', 'sorted.csv')result = open('sorted.csv')print(result.read())result.close()city/month,Jan,Feb,Mar,AprBrisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Melbourne,41.2,35.5,37.4,29.3​Note that the row for Melbourne has been sorted below the rows for Brisbane and Darwin because Melbourne comes after Brisbane and Darwin, based on alphabetical ordering.

You want to write a program that prints the average of the high temperatures in January. Which of the four file-reading approaches should you use?

Create a stem plot. (Enter numbers from smallest to largest separated by spaces. Enter NONE for stems with no values.)The data are daily high temperatures in a town for one month.60, 60, 61, 63, 64, 65, 65, 65, 67, 68, 70, 70, 70, 71, 71, 72, 74, 74, 74, 75, 75, 75, 76, 76, 77, 78, 78, 79, 79, 95

write a function read_data(filename) that retrieves the data from the given file filename (the file is in CSV format) by returning every entry INCLUDING the headings (the headings always exist in given files, this will be used later for finding indices of columns) as a single list. Each entry should be a list containing data separated by a comma, and any new line characters at the end of each row should be stripped off. It's important to note that no imports should be used for this task.You may assume there are no errors in the given and tested files.For example:Test Input Resultdata = read_data('CreditCard_2024.csv')print(data[0])CreditCard_2024.csvTransactionID,TimeStamp,CardNumber,Transaction_type,Type_of_card,Entry_mode,Amount,Actual,Origin,Class

Create a stem plot. (Enter numbers from smallest to largest separated by spaces. Enter NONE for stems with no values.)The data are daily high temperatures in a town for one month.61, 61, 63, 64, 65, 66, 66, 66, 67, 68, 70, 70, 70, 71, 71, 72, 74, 74, 74, 75, 75, 75, 76, 76, 77, 78, 78, 79, 79, 99

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.