NKing
NKing

Reputation: 23

Parsing data from a file

I have been provided with a file containing data on recorded sightings of species, which is laid out in the format;

"Species", "\t", "Latitude", "\t", "Longitude"

I need to define a function that will load the data from the file into a list, whilst for every line in the list spiting it into three components, species name, latitude and longitude.

This is what i have but it is not working:

def LineToList(FileName):
    FileIn = open(FileName, "r")
    DataList = []
    for Line in FileIn:
        Line = Line.rstrip()
        DataList.append(Line)
        EntryList = []
        for Entry in Line:
            Entry = Line.split("\t")
            EntryList.append(Entry)
    FileIn.close()
    return DataList

LineToList("Mammal.txt")
print(DataList[1])

I need the data on each line to be separated so that i can use it later to calculate where the species was located within a certain distance of a given location.

Sample Data:

Myotis nattereri    54.07663633 -1.006446707
Myotis nattereri    54.25637837 -1.002130504
Myotis nattereri    54.25637837 -1.002130504

I am Trying to print one line of the data set to test if it is splittiing correctly but nothing is showing in the shell

Update:

This is the code i am working with now;

def LineToList(FileName):
    FileIn = open(FileName, "r")
    DataList = []
    for Line in FileIn:
        Line = Line.rstrip()
        DataList.append(Line)
        EntryList = []
        for Entry in Line:
            Entry = Line.split("\t")
            EntryList.append(Entry)
            return EntryList
    FileIn.close()
    return DataList

def CalculateDistance(Lat1, Lon1, Lat2, Lon2):

    Lat1 = float(Lat1)
    Lon1 = float(Lon1)
    Lat2 = float(Lat2)
    Lon2 = float(Lon2)

    nDLat = (Lat1 - Lat2) * 0.017453293
    nDLon = (Lon1 - Lon2) * 0.017453293

    Lat1 = Lat1 * 0.017453293
    Lat2 = Lat2 * 0.017453293

    nA = (math.sin(nDLat/2) ** 2) + math.cos(Lat1) * math.cos(Lat2) * (math.sin(nDLon/2) ** 2 )
    nC = 2 * math.atan2(math.sqrt(nA),math.sqrt( 1 - nA ))
    nD = 6372.797 * nC

    return nD

DataList = LineToList("Mammal.txt")                
for Line in DataList:
    LocationCount = 0
    CalculateDistance(Entry[1], Entry[2], 54.988056, -1.619444)
    if CalculateDistance <= 10:
        LocationCount += 1
    print("Number Recordings within Location Range:", LocationCount)

When running the programme come up with an error:

CalculateDistance(Entry[1], Entry[2], 54.988056, -1.619444) NameError: name 'Entry' is not defined

Upvotes: 2

Views: 388

Answers (3)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210972

I saw "Biological Sciences" in your profile and just because of that i would recommend you to take a closer look at Pandas module.

It can be very easy:

import pandas as pd

df = pd.read_csv('mammal.txt', sep='\t',
                 names=['species','lattitude','longitude'],
                 header=None)

print(df)

Output:

            species  lattitude  longitude
0  Myotis nattereri  54.076636  -1.006447
1  Myotis nattereri  54.256378  -1.002131
2  Myotis nattereri  54.256378  -1.002131

Upvotes: 2

tdelaney
tdelaney

Reputation: 77407

I think you have a regular tab-delimited CSV that csv.reader can easily parse for you.

import csv
DataList = [row for row in csv.reader(open('Mammal.txt'), dialect='excel-tab')]
for data in DataList:
    print(data)

This results in

['Myotis nattereri', '54.07663633', '-1.006446707']
['Myotis nattereri', '54.25637837', '-1.002130504']
['Myotis nattereri', '54.25637837', '-1.002130504']

Upvotes: 1

user447688
user447688

Reputation:

Your DataList variable is local to the LineToList function; you have to assign to another variable at file scope:

DataList = LineToList("Mammal.txt")
print(DataList[1])

Upvotes: 1

Related Questions