Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Files handling and lists
#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


Messages In This Thread
Files handling and lists - by gonzo620 - Oct-04-2018, 07:51 PM
RE: Files handling and lists - by buran - Oct-04-2018, 07:56 PM
RE: Files handling and lists - by gonzo620 - Oct-04-2018, 07:57 PM
RE: Files handling and lists - by snippsat - Oct-04-2018, 09:39 PM
RE: Files handling and lists - by gonzo620 - Oct-05-2018, 03:21 PM
RE: Files handling and lists - by buran - Oct-05-2018, 07:22 AM
files handling, if statements, and lists - by gonzo620 - Oct-08-2018, 05:57 AM
files, lists, and if statements - by gonzo620 - Oct-08-2018, 10:01 PM
RE: files, lists, and if statements - by ichabod801 - Oct-08-2018, 10:08 PM
RE: files, lists, and if statements - by gonzo620 - Oct-08-2018, 11:06 PM
RE: files, lists, and if statements - by ichabod801 - Oct-09-2018, 12:52 AM
RE: files, lists, and if statements - by gonzo620 - Oct-09-2018, 01:29 AM
RE: files, lists, and if statements - by ichabod801 - Oct-09-2018, 01:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,727 Nov-25-2023, 05:31 AM
Last Post: Iqratech
Star python exception handling handling .... with traceback mg24 3 1,380 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Handling pdf files with python. fuzzin 1 1,333 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,927 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,524 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Handling Large XML Files (>10GB) in Python onlydibs 1 4,328 Dec-22-2019, 05:46 AM
Last Post: Clunk_Head
  How to concatenate files while looping through lists? python_newbie09 3 2,981 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,413 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