需要帮助解决:IndexError:列表索引超出范围
代码是阅读文本中的信息并打印出城市及其最高温度。一旦完成,它应该关闭文件。
请帮助。非常感谢。
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
temp_file = open('mean_temp.txt','a+')
temp_file.write("Rio de Janeiro,Brazil,30.0,18.0")
temp_file.seek(0,0)
headings = temp_file.readline()
headings = headings.split(',')
#print(headings)
city_temp = temp_file.readline()
while city_temp:
city_temp = temp_file.readline()
city_temp = city_temp.split(',')
print(headings[0].capitalize(),"of",city_temp[1],headings[2],"is",city_temp[2],"Celsius.")
temp_file.close()
以下是temp_file.txt的内容:
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
答案 0 :(得分:0)
问题在于您的文本文件,例如我初始化的假设。您已将初始数据转储到一行中,因此所有内容都被读入headings
并且行已耗尽。
在这里,修好它:
temp_file = open('mean_temp.txt','a+')
temp_file.write("Rio de Janeiro,Brazil,30.0,18.0\n")
temp_file.seek(0,0)
headings = temp_file.readline()
headings = headings.split(',')
city_temp = temp_file.readline()
while city_temp:
city_temp = city_temp.split(',')
print(headings[0].capitalize(),"of",city_temp[1],headings[2],"is",city_temp[2],"Celsius.")
city_temp = temp_file.readline()
temp_file.close()
并通过在每个已经存在的国家/地区之后添加新行来更改mean_temp.txt
。
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
这是输出:
City of China month ave: highest high is 30.9 Celsius.
City of Egypt month ave: highest high is 34.7 Celsius.
City of UK month ave: highest high is 23.5 Celsius.
City of Kenya month ave: highest high is 26.3 Celsius.
City of USA month ave: highest high is 28.9 Celsius.
City of Australia month ave: highest high is 26.5 Celsius.
City of Japan month ave: highest high is 30.8 Celsius.
City of Brazil month ave: highest high is 30.0 Celsius.
答案 1 :(得分:-1)
试试这个
f = open("ml/play.txt", "r")
for line in f:
x = line.split(',')
print(x[0].capitalize(),"of",x[1],x[2],"is",x[3],"Celsius.")
答案 2 :(得分:-1)
问题在于你完成while
循环的方式。您正在跳过第一个city_temp
行,因为在循环内部,您会立即将其从下一行重新分配。
当您到达文件末尾时,city_temp
将为空,但您仍会尝试将其拆分并打印字段。
更改此代码:
city_temp = temp_file.readline()
while city_temp:
city_temp = temp_file.readline()
city_temp = city_temp.split(',')
print(headings[0].capitalize(),"of",city_temp[1],headings[2],"is",city_temp[2],"Celsius.")
为:
while True:
city_temp = temp_file.readline()
if not city_temp:
break
city_temp = city_temp.split(',')
print(headings[0].capitalize(),"of",city_temp[1],headings[2],"is",city_temp[2],"Celsius.")
顺便说一下,Python有一个用于读写CSV文件的模块,你不需要自己编写代码。见https://docs.python.org/3/library/csv.html
答案 3 :(得分:-2)
如果您正在对任何抽象数据类型执行索引操作,并且如果您不知道或不确定抽象数据类型中的索引总数,那么随时将您的索引操作放在try except块中。而你的except块应该处理索引错误异常。
try:
print(headings[0].capitalize(),"of",city_temp[1],headings[2],"is",city_temp[2],"Celsius.")
except IndexError:
print "Index Error"