Python 使用while循环从文件打印
我目前正在学习python,在练习的某个部分遇到了困难: 使用!要下载的curl作为mean_temp.txtPython 使用while循环从文件打印,python,python-3.x,Python,Python 3.x,我目前正在学习python,在练习的某个部分遇到了困难: 使用!要下载的curl作为mean_temp.txt #[]天气:导入world\u mean\u team.csv作为mean\u temp.txt 以“r”模式打开文件。 将第一行文本读入名为:headers和print()的变量。 使用在每个逗号上拆分的.split(',)将标题转换为列表,print()列表。 #[]天气:打开文件,读取/打印第一行,将行转换为列表(以逗号分隔) 使用while循环读取文件中的其余行 将剩余的行分配
#[]天气:导入world\u mean\u team.csv作为mean\u temp.txt 以“r”模式打开文件。 将第一行文本读入名为:headers和
print()
的变量。
使用在每个逗号上拆分的.split(',)
将标题转换为列表,print()
列表。#[]天气:打开文件,读取/打印第一行,将行转换为列表(以逗号分隔) 使用while循环读取文件中的其余行 将剩余的行分配给
city\u temp
变量。
对于循环中的每个.readline()
,使用.split(',')
将城市温度转换为列表。
打印每个城市的最高月平均温度。
关闭平均时间
提示和提示:
使用标题的打印输出来确定要使用的city\u temp
索引。
北京的“月平均气温:最高”为30.9摄氏度。
将city\u temp
转换为带有.split(',')
的列表
#[]天气:使用while循环打印城市和最高月平均温度(摄氏度)
第一部分我做得很好,但是第二部分是用temp打印每个城市是困难的,我不知道如何解决它
到目前为止,我的代码是:
mean_temp = open('mean_temp.txt', 'r')
read_line = mean_temp.readline()
print(read_line)
heading = read_line.split(',')
print('Heading list: ',heading)
city_temp = ''
while read_line:
print(read_line[:-1]) # writes the first line in the file
read_line = mean_temp.readline() # goes to the next line
city_temp += read_line # adding the line to the variable
city_temp1 = city_temp.split(',') # split the line every comma to a list
print(city_temp1)
输出为:
city,country,month ave: highest high,month ave: lowest low
Heading list: ['city', 'country', 'month ave: highest high', 'month ave: lowest low\n']
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
['Beijing', 'China', '30.9', '-8.4\nCairo', 'Egypt', '34.7', '1.2\nLondon', 'UK', '23.5', '2.1\nNairobi', 'Kenya', '26.3', '10.5\nNew York City', 'USA', '28.9', '-2.8\nSydney', 'Australia', '26.5', '8.7\nTokyo', 'Japan', '30.8', '0.9\n']
while循环应该将该行拆分为一个列表,然后使用正确的整数索引该列表,以提取城市和高温信息。例如:
mean_temp = open('mean_temp.txt', 'r')
read_line = mean_temp.readline()
heading = read_line.split(',')
print('Heading list: ',heading)
# prints "Heading list: ['city', 'country', 'month ave: highest high', 'month ave: lowest low\n']",
# which means index ZERO and index TWO is the correct values to pull from each list created from each line
# Move onto the next line, we want to start the while loop with the first city
city_temp = mean_temp.readline() # goes to the next line
while city_temp:
# print(read_line[:-1]) # don't write the whole line to the console, we want to print just the CITY and HIGH TEMP
city_info = city_temp.split(',') # This is the list
print(city_info[0] + ': ' + city_info[2])
city_temp = mean_temp.readline() # go to the next line at the end of the while loop
# make sure to close the file like the instructions said
mean_temp.close()
上述代码的输出为:
Heading list: ['city', 'country', 'month ave: highest high', 'month ave: lowest low\n']
Beijing: 30.9
Cairo: 34.7
London: 23.5
Nairobi: 26.3
New York City: 28.9
Sydney: 26.5
Tokyo: 30.8
请用一个描述你的问题的问题将你的帖子浓缩成一篇文章。您已经解决了部分任务—无需在此重复—只需创建一个最小的示例。代码墙和/或描述性文本会导致否决票-它淹没了您想要在文本中获得的东西,我们需要深入研究才能得出您的观点。@Itay123如果它解决了您的问题,请接受答案。谢谢