Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Files handling and lists
#1
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?
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?)
Reply
#4
(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.
Reply
#5
(Oct-04-2018, 09:39 PM)snippsat Wrote: next(f) in Python 3 Wink
Yeah, my bad. Sorry
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
Reply
#7
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?
Reply
#8
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?
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,547 Nov-25-2023, 05:31 AM
Last Post: Iqratech
Star python exception handling handling .... with traceback mg24 3 1,267 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Handling pdf files with python. fuzzin 1 1,260 Jan-19-2022, 02:24 PM
Last Post: ThiefOfTime
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,806 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,372 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Handling Large XML Files (>10GB) in Python onlydibs 1 4,205 Dec-22-2019, 05:46 AM
Last Post: Clunk_Head
  How to concatenate files while looping through lists? python_newbie09 3 2,872 Mar-24-2019, 03:11 PM
Last Post: python_newbie09
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,274 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020