-5

Need help to solve: IndexError: list index out of range

The code is to read the info in the text and print out the city and their highest temp. And once all is done, it should close the file.

Assistance please. Much thanks.

!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()

Here are the contents of 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
Harshith Thota
  • 856
  • 8
  • 20

4 Answers4

0

The problem was with your text file like I initialled assumed. You've dumped the intial data into a single line and hence everything was being read into headings and the lines got exhausted. Here, fixed it:

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()

and change the mean_temp.txt by adding a new line after every country that's already present in that.

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 

Here is the output:

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.
Harshith Thota
  • 856
  • 8
  • 20
  • Note that your code is still not achieving what you were suppossed to do. Especially, printing the capitals and those extra words. Consider replacing `headings[0]` in the `print` statement with `city_temp[0]`. – Harshith Thota Jul 10 '17 at 06:13
-1

Try this

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.")
Sunny
  • 179
  • 1
  • 8
-1

The problem is with the way you've done your while loop. You're skipping the first city_temp line, because inside the loop you immediately reassign it from the next line.

And when you reach the end of the file, city_temp will be empty, but you'll still try to split it and print the fields.

Change this code:

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.")

to:

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.")

BTW, Python has a module for reading and writing CSV files, you don't need to code this yourself. See https://docs.python.org/3/library/csv.html

Barmar
  • 741,623
  • 53
  • 500
  • 612
-2

Any time if you are doing index operations on any abstract data type and with that if you are unknown or unsure of total number of indexes in your abstract data type, then put your index operation in a try except block. And your except block should handle index error exception.

try:
   print(headings[0].capitalize(),"of",city_temp[1],headings[2],"is",city_temp[2],"Celsius.")
except IndexError:
   print "Index Error"
Floern
  • 33,559
  • 24
  • 104
  • 119