Aug-05-2018, 04:29 PM
Hello,
My Module 4 Midterm for Python Fundamentals on edX involves the following instructions:
***************************************************
Create a program that:
imports and opens a file
appends additional data to a file
reads from the file to displays each city name and month average high temperature in Celsius
Output: The output should resemble the following
City of Beijing month ave: highest high is 30.9 Celsius
City of Cairo month ave: highest high is 34.7 Celsius
City of London month ave: highest high is 23.5 Celsius
City of Nairobi month ave: highest high is 26.3 Celsius
City of New York City month ave: highest high is 28.9 Celsius
City of Sydney month ave: highest high is 26.5 Celsius
City of Tokyo month ave: highest high is 30.8 Celsius
City of Rio De Janeiro month ave: highest high is 30.0 Celsius
all of the above text output is generated from the file
the only strings are hard coded:
"is"
"of"
"Celsius"
import the file into the Jupyter Notebook environment
use !curl to download https://raw.githubusercontent.com/Micros...p_mean.csv as mean_temp.txt
# [ ] The Weather: import world_mean_team.csv as mean_temp.txt into the Jupyter notebook
Add the weather for Rio
Open the file in append plus mode ('a+')
Write a new line for Rio de Janeiro "Rio de Janeiro,Brazil,30.0,18.0\n"
Grab the column headings
use .seek() to move the pointer to the beginning of the file
read the first line of text into a variable called: headings
convert headings to a list using .split(',') which splits on each comma
# [ ] The Weather: open file, read/print first line, convert line to list (splitting on comma)
Read the remaining lines from the file using a while loop
assign remaining lines to a city_temp variable
convert the city_temp to a list using .split(',') for each .readline() in the loop
print each city & the highest monthly average temperature
close mean_temps
Tips & Hints:
print headings to determine indexes to use for the final output (what is in headings[0], [1], [2]..?)
the city_temp data follows the order of the headings (city_temp[0] is described by headings[0])
The output should look like: "month ave: highest high" for Beijing is 30.9 Celsius
convert city_temp to lists with .split(',')
# [ ] The Weather: use while loop to print city and highest monthly average temp in celsius
*****************************************************
I have most of the code written and functioning up until the part involving the while loop with .readlines() and assigning the file to the city_temp variable (second to last paragraph in the instructions). I do not know how to set up the while loop, for city_temp I can only get it to read the second line of the file and to have it use the .split(",") properly. When I try to use city_temp = mean_temp.readlines().split(",") I constantly get an error message. Is there a way to overcome this issue by setting up the required while loop a certain way? Any help would be greatly appreciated, thank you!
*****************************************************
Here is my code so far:
My Module 4 Midterm for Python Fundamentals on edX involves the following instructions:
***************************************************
Create a program that:
imports and opens a file
appends additional data to a file
reads from the file to displays each city name and month average high temperature in Celsius
Output: The output should resemble the following
City of Beijing month ave: highest high is 30.9 Celsius
City of Cairo month ave: highest high is 34.7 Celsius
City of London month ave: highest high is 23.5 Celsius
City of Nairobi month ave: highest high is 26.3 Celsius
City of New York City month ave: highest high is 28.9 Celsius
City of Sydney month ave: highest high is 26.5 Celsius
City of Tokyo month ave: highest high is 30.8 Celsius
City of Rio De Janeiro month ave: highest high is 30.0 Celsius
all of the above text output is generated from the file
the only strings are hard coded:
"is"
"of"
"Celsius"
import the file into the Jupyter Notebook environment
use !curl to download https://raw.githubusercontent.com/Micros...p_mean.csv as mean_temp.txt
# [ ] The Weather: import world_mean_team.csv as mean_temp.txt into the Jupyter notebook
Add the weather for Rio
Open the file in append plus mode ('a+')
Write a new line for Rio de Janeiro "Rio de Janeiro,Brazil,30.0,18.0\n"
Grab the column headings
use .seek() to move the pointer to the beginning of the file
read the first line of text into a variable called: headings
convert headings to a list using .split(',') which splits on each comma
# [ ] The Weather: open file, read/print first line, convert line to list (splitting on comma)
Read the remaining lines from the file using a while loop
assign remaining lines to a city_temp variable
convert the city_temp to a list using .split(',') for each .readline() in the loop
print each city & the highest monthly average temperature
close mean_temps
Tips & Hints:
print headings to determine indexes to use for the final output (what is in headings[0], [1], [2]..?)
the city_temp data follows the order of the headings (city_temp[0] is described by headings[0])
The output should look like: "month ave: highest high" for Beijing is 30.9 Celsius
convert city_temp to lists with .split(',')
# [ ] The Weather: use while loop to print city and highest monthly average temp in celsius
*****************************************************
I have most of the code written and functioning up until the part involving the while loop with .readlines() and assigning the file to the city_temp variable (second to last paragraph in the instructions). I do not know how to set up the while loop, for city_temp I can only get it to read the second line of the file and to have it use the .split(",") properly. When I try to use city_temp = mean_temp.readlines().split(",") I constantly get an error message. Is there a way to overcome this issue by setting up the required while loop a certain way? Any help would be greatly appreciated, thank you!
*****************************************************
Here is my code so far:
In [1]: # The Weather: import world_mean_temp.csv as mean_temp.txt into the Jupyter Notebook ! curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt mean_temp = open("mean_temp.txt", "r") print("\n\n" + mean_temp.read()) % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 222 100 222 0 0 125 0 0:00:01 0:00:01 --:--:-- 125 city,country,month ave: highest high,month ave: lowest low Beijing,China,30.9,-8.4 Cairo,Egypt,34.7,1.2 London,UK,23.5,2.1 Nairobi,Kenya,26.3,10.5 New York City,USA,28.9,-2.8 Sydney,Australia,26.5,8.7 Tokyo,Japan,30.8,0.9 In [8]: mean_temp = open("mean_temp.txt", "a+") mean_temp.write("Rio de Janeiro,Brazil,30.0,18.0\n") Out[8]: 32 In [9]: mean_temp.seek(0) headings = mean_temp.readline().split(",") print(headings) ['city', 'country', 'month ave: highest high', 'month ave: lowest low\n'] In [10]: city_temp = mean_temp.readline().split(",") city_temp Out[10]: ['Beijing', 'China', '30.9', '-8.4\n']