0
ValueError: invalid literal for int() with base 10: 'temperature(F)\n'

i want to skip the header row while reading and im getting an error because it is reading the header.

arr = []    
with open('D:\\ml_learning\\data-structures-algorithms-python-master\\data_structures\\4_HashTable_2_Collisions\\Solution\\nyc_weather.csv', 'r') as file:
    for line in file:
        tokens = line.split(',')
        try:
            temperature = int(tokens[1])
            arr.append(temperature)
        except Exception as e:
            print('invalid temperature'+e)

1 Answers1

0

Try using file.readlines() and then slice it to skip the first line:

arr = []    
with open('D:\\ml_learning\\data-structures-algorithms-python-master\\data_structures\\4_HashTable_2_Collisions\\Solution\\nyc_weather.csv', 'r') as file:
    lines = file.readlines()
    for line in lines[1:]:
        tokens = line.split(',')
        try:
            temperature = int(tokens[1])
            arr.append(temperature)
        except Exception as e:
            print('invalid temperature'+e)

Alternatively, have a look at the pandas package, which is specifically made for reading, writing and manipulating file data. https://www.w3schools.com/python/pandas/pandas_csv.asp