Problem #1: Read in a CSV (Comma-Separated Values) File For this problem, you need to read in the “countries.csv” file w
Posted: Wed Apr 27, 2022 3:13 pm
Problem #1: Read in a CSV (Comma-Separated Values) File For this
problem, you need to read in the “countries.csv” file which was
provided along with this assignment. It contains information from
Wikipedia about the countries in the world, one line per country.
The first five lines of that file look like this:
Luxembourg,Europe,122740,634730,2586
Singapore,Asia,102742,5453600,728
Ireland,Europe,99239,5011500,70273 Qatar,Asia,97262,2799202,11586
Switzerland,Europe,75880,8717105,41284 The first line of the file
means that Luxembourg is a country in Europe with a per-capita
gross domestic product (GDP) of $122,740. (This is a measure of how
wealthy a country is.) It has a population of 634,730 and an area
(in square kilometers) of 2586. Subsequent lines have a similar
meaning for each country. Write a function called readData which
takes no arguments. It should open the countries.csv file for
reading, read the file, and return a variable called “data” which
should be a list of lists containing all the data in the file, the
beginning of which should look like the following: [['Luxembourg',
'Europe', 122740, 634730, 2586], ['Singapore', 'Asia', 102742,
5453600, 728], ['Ireland', 'Europe', 99239, 5011500, 70273],
['Qatar', 'Asia', 97262, 2799202, 11586], ['Switzerland', 'Europe',
75880, 8717105, 41284] … Here are some hints on how to do this
problem: 1. If you use the readlines() method on a file object, you
will get a list of strings containing the lines in the file. 2.
Each of these lines will be terminated with a newline character
“\n”; you can remove this with the strip() method, which works on a
string to strip whitespace from either side of the string (newline
characters are considered whitespace, along with spaces and tabs.)
Or you can use string slicing. 3. To split each line into a list
using a comma as the delimiter, use the split(“,”) method which
works on a string. 4. After splitting, all the five elements of the
list representing a line of the file will still be strings. You
need to convert the third, fourth, and fifth elements of this list
(the GDP per capita, population, and area respectively) to integers
using the int() function.
problem, you need to read in the “countries.csv” file which was
provided along with this assignment. It contains information from
Wikipedia about the countries in the world, one line per country.
The first five lines of that file look like this:
Luxembourg,Europe,122740,634730,2586
Singapore,Asia,102742,5453600,728
Ireland,Europe,99239,5011500,70273 Qatar,Asia,97262,2799202,11586
Switzerland,Europe,75880,8717105,41284 The first line of the file
means that Luxembourg is a country in Europe with a per-capita
gross domestic product (GDP) of $122,740. (This is a measure of how
wealthy a country is.) It has a population of 634,730 and an area
(in square kilometers) of 2586. Subsequent lines have a similar
meaning for each country. Write a function called readData which
takes no arguments. It should open the countries.csv file for
reading, read the file, and return a variable called “data” which
should be a list of lists containing all the data in the file, the
beginning of which should look like the following: [['Luxembourg',
'Europe', 122740, 634730, 2586], ['Singapore', 'Asia', 102742,
5453600, 728], ['Ireland', 'Europe', 99239, 5011500, 70273],
['Qatar', 'Asia', 97262, 2799202, 11586], ['Switzerland', 'Europe',
75880, 8717105, 41284] … Here are some hints on how to do this
problem: 1. If you use the readlines() method on a file object, you
will get a list of strings containing the lines in the file. 2.
Each of these lines will be terminated with a newline character
“\n”; you can remove this with the strip() method, which works on a
string to strip whitespace from either side of the string (newline
characters are considered whitespace, along with spaces and tabs.)
Or you can use string slicing. 3. To split each line into a list
using a comma as the delimiter, use the split(“,”) method which
works on a string. 4. After splitting, all the five elements of the
list representing a line of the file will still be strings. You
need to convert the third, fourth, and fifth elements of this list
(the GDP per capita, population, and area respectively) to integers
using the int() function.