Python Forum

Full Version: Files handling and lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
im creating a program that will tell me what the climate is in a city in the summer and in the winter based on it's latitude. I'm doing this by adding them to separate lists first. How can I have my program skip the first line because it is the header.

my code
with open('cities.txt','r', encoding = 'cp1252') as f:
    
    season = input('Will you take your holiday in the summer or winter? ')
    temp = input('Do you want to go to a destination that is warm or cold? ')

    summercold = []
    summerwarm = []
    winterwarm = []
    wintercold = []


    for line in f:
        lat = int(line[0:2])
        
        if lat > 66:
            summercold.append(line)
            wintercold.append(line)
            
        elif lat > 35 and lat < 66:
            if line[5] == N:
                summerwarm.append(line)
                wintercold.append(line)
            elif line[5] == S:
                summercold.append(line)
                winterwarm.append(line)
                
        else:
            summerwarm.append(line)
            winterwarm.append(line)
Error:
lat = int(line[0:2]) ValueError: invalid literal for int() with base 10: 'La'
I'm getting an error because the first line is a string so line[0:2] can't convert it into an integer. How can I skip the first line?
One option is to use f.next() once, after opening the file.
However, better have a look at csv module and use DictReader. It will be more convenient to work with different fields on each row
Also, is there an easier way to do this other than lists?
i.e: can I print the element in the 3rd column directly(I know you can print lines but does it work with columns?)
(Oct-04-2018, 07:56 PM)buran Wrote: [ -> ]One option is to use f.next() once, after opening the file.
next(f) in Python 3 Wink

gonzo620 Wrote:i.e: can I print the element in the 3rd column directly(I know you can print lines but does it work with columns?)
You don't have any column only lines,have to split the lines.
Then line[2] will get 3rd column,or fix your error in code over.
for line in f:
    line = line.split('\t')
    #print(line)
    lat = line[0:2]
    print(lat)
Output:
['Latitude', 'Longitude'] ['82°30N', '62°20W'] ['81°36N', '16°40W'] ['79°59N', '85°56W'] ['78°55N', '11°56E'] ....
gonzo620 Wrote:Also, is there an easier way to do this other than lists?
Working with data in this way is very inconvenient.
As @buran posted you should try to get data in a structure like eg a dictionary,
using csv module and and DictReader.
(Oct-04-2018, 09:39 PM)snippsat Wrote: [ -> ]next(f) in Python 3 Wink
Yeah, my bad. Sorry
gonzo620 Wrote:i.e: can I print the element in the 3rd column directly(I know you can print lines but does it work with columns?)
You don't have any column only lines,have to split the lines.
Then line[2] will get 3rd column,or fix your error in code over.
for line in f:
    line = line.split('\t')
    #print(line)
    lat = line[0:2]
    print(lat)
Output:
['Latitude', 'Longitude'] ['82°30N', '62°20W'] ['81°36N', '16°40W'] ['79°59N', '85°56W'] ['78°55N', '11°56E'] ....
Now is there a way to slice these? I need to assign the numerical values as separate values and turn them into integers in order to use them in an equation
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?
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)
How can I fix this?
Once you read a file it's done. You read fin on line 2, putting it into data. So when you try to read it again on line 11, there's nothing left, and your loop to build the lists does nothing. The loop should probably be through data, not fin.
(Oct-08-2018, 10:08 PM)ichabod801 Wrote: [ -> ]Once you read a file it's done. You read fin on line 2, putting it into data. So when you try to read it again on line 11, there's nothing left, and your loop to build the lists does nothing. The loop should probably be through data, not fin.

when I do the loop through data then an error happens with the integer commands. fout is supposed to remove the first line because it is the header so this error doesn't happen. and the last part of this code adds that header line back in. Any suggestions?
Pages: 1 2