pythongeonames

how to extract specific content from a text file in python?


I am using the geonames zip code data file at this link. A sample data from above file is as below:

IT  67010   Barete  Abruzzi AB  L'Aquila    AQ          42.4501 13.2806 4
IT  67012   Cagnano Amiterno    Abruzzi AB  L'Aquila    AQ          42.4574 13.2279 4
IT  67012   San Giovanni    Abruzzi AB  L'Aquila    AQ          42.4642 13.2584 4
IT  67013   Mascioni    Abruzzi AB  L'Aquila    AQ          42.5282 13.3492 4
IT  67013   Campotosto  Abruzzi AB  L'Aquila    AQ          42.5588 13.368  4
IT  67013   Poggio Cancelli Abruzzi AB  L'Aquila    AQ          42.5623 13.327  4
IT  67013   Ortolano    Abruzzi AB  L'Aquila    AQ          42.5193 13.4238 4
IT  67014   Capitignano Abruzzi AB  L'Aquila    AQ          42.5204 13.3012 4
IT  67015   Montereale  Abruzzi AB  L'Aquila    AQ          42.5233 13.2459 4
IT  67015   Marana Di Montereale    Abruzzi AB  L'Aquila    AQ          42.4967 13.2236 4
IT  67015   San Giovanni Paganica   Abruzzi AB  L'Aquila    AQ          42.5057 13.2762 4
IT  67015   Aringo  Abruzzi AB  L'Aquila    AQ          42.5554 13.2671 4
IT  67015   Ville Di Fano   Abruzzi AB  L'Aquila    AQ          42.5248 13.1864 4
IT  67015   Cesaproba   Abruzzi AB  L'Aquila    AQ          42.4985 13.1864 4
IT  67015   Marana  Abruzzi AB  L'Aquila    AQ          42.4967 13.2236 4

In the above file the second section in each line(the 5 digit numeric code after IT) is the zip code. The second last and third last fields in each line(the 2 float numbers towards the end) are the latitude and longitude.

I am trying to extract these 3 fields and am having some difficulty especially for latitude and longitude part. Here is the code I have written so far:

    zip_code = ''
    latitude = ''
    longitude = ''
    file1 = open("IT.txt", "r")
    for line in file1:
        line = line.rstrip('\n')
        zip_code = line[3:8]
        latitude = line[-17:-10]
        longitude = line[-9:-2]
        print(latitude)
        print(longitude)

Below is some of the sample output for latitude and longitude:

Latitude:

42.4501
42.4574
42.4642
42.5282
    42.558
    42.562
42.5193
42.5204

Longitude

13.2806
13.2279
13.2584
13.3492
    13.368
    13.327
13.4238
13.3012

As you can some of the latitude and longitude are not of standard length(Majority of the latitude and longitude have 4 digits after decimal but some have only 3). This is causing issue in proper formatting. How can I extract the latitude and longitude fields from each line in above file?


Solution

  • You were pretty close!

    I would just suggest indexing in directly since you know which column they are instead of trying to parse by slicing:

    with open('data.txt', 'r') as f:
        data = f.readlines()
    
    
    for line in data:
        line_sequence = line.split()
    
        zipcode, lat, long = line_sequence[1], line_sequence[-2], line_sequence[-3]
    
        print zipcode, lat, long
    

    output:

    67010 13.2806 42.4501
    67012 13.2279 42.4574
    67012 13.2584 42.4642
    67013 13.3492 42.5282
    67013 13.368 42.5588
    67013 13.327 42.5623
    67013 13.4238 42.5193
    67014 13.3012 42.5204
    67015 13.2459 42.5233
    67015 13.2236 42.4967
    67015 13.2762 42.5057
    67015 13.2671 42.5554
    67015 13.1864 42.5248
    67015 13.1864 42.4985
    67015 13.2236 42.4967