Oct-08-2018, 05:57 AM
I am trying to read a text file and add each row into an appropriate list, but my lists return empty
with open('cities.txt','r', encoding = 'cp1252') as fin: # opens file with read permission data = fin.read().splitlines(True) with open('cities.txt','w', encoding = 'cp1252') as fout: fout.writelines(data[1:]) # gives write permission and deletes the first line coldsummer = [] # these are empty lists that will be filled in later warmsummer = [] coldwinter = [] warmwinter = [] for line in fin: line = line.split('\t') # splits elements in line with a tab lat = line[0] # these assign variables to certain parts of the file long = line[1] city = line[2] lat_deg = int(lat[0:2]) # splices and converts these values to integers lat_min = int(lat[3:5]) long_deg = int(long[0:2]) long_min = int(long[3:5]) if lat_deg > 66: # these if statements will fill in lists with appropriate values coldsummer.append(line) coldwinter.append(line) elif lat_deg > 35 and lat_deg < 66: if lat[5] == N: warmsummer.append(line) coldwinter.append(line) else: warmsummer.append(line) warmwinter.append(line) else: warmsummer.append(line) warmwinter.append(line) with open('cities.txt','r+', encoding = 'cp1252') as f: # adds the first line back heading = 'Latitude\tLongitude\tCity\tProvince/State\tCountry' filedata = f.read() f.seek(0, 0) f.write(heading.rstrip('\r\n') + '\n' + filedata)What am I doing wrong?