**Required Weather Program:**
The Weather
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/MicrosoftLearning/intropython/master/world_temp_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
我的代码:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
exam_file=open('mean_text.txt', 'a+')
exam_file.seek(0,0)
city_temp = exam_file.readline().split(",")
citystemp = exam_file.readline().split(",")
citytemp = exam_file.readline().split(",")
city_tem = exam_file.readline().split(",")
citystem = exam_file.readline().split(",")
citytem = exam_file.readline().split(",")
city_te = exam_file.readline().split(",")
print("City of", city_temp[0], "month ave: highest high is", city_temp[2], "Celsius")
print("City of", citystemp[0], "month ave: highest high is", citystemp[2], "Celsius")
print("City of", citytemp[0], "month ave: highest high is", citytemp[2], "Celsius")
print("City of", city_tem[0], "month ave: highest high is", city_tem[2], "Celsius")
print("City of", citystem[0], "month ave: highest high is", citystem[2], "Celsius")
print("City of", citytem[0], "month ave: highest high is", citytem[2], "Celsius")
print("City of", city_te[0], "month ave: highest high is", city_te[2], "Celsius")
exam_file.write("Rio de Janeiro, Brazil, 30.0, 18.0, \n")
cityste = exam_file.readline().split(",")
print("City of", cityste[0], "month ave: highest high is", cityste[2], "Celsius")
exam_file.seek(0)
exam_file.close()
exam_file = open("mean_text.txt","r")
exam = exam_file.read
print(exam)
我的输出:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 222 100 222 0 0 991 0 --:--:-- --:--:-- --:--:-- 1072
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius
<built-in method read of _io.TextIOWrapper object at 0x7fdd00fad558>
即使我附加了Rio而不是替换其他内容,为什么它会一遍又一遍地显示相同的内容
答案 0 :(得分:0)
因为所有文件都一样?
exam_file.readline().split(",")
又因为您总是在读取相同列表的相同信息?
print("City of", city_temp[0], "month ave: highest high is", city_temp[2], "Celsius")
应该类似于:
city_temp=["Rio de Janeiro", "something", "something", "30.0"]
因此,您总是选择city_temp[0]
是里约热内卢,而city_temp[2]
是“ 30.0”。
我真的不明白你的例子。
答案 1 :(得分:0)
您可以使用
从文件中读取所有行alllines = exam_file.readlines()
header = alllines.pop(0)
遍历循环的其余行
for line in alllines
多次调用readline()
来分别读取每一行并不是一种干净的方法。