0

I have a text file containing this data and I need to make a function wherein this .txt file is opened, each line is read and then transferred into a python dictionary. This dictionary will have the first columns as keys and then the corresponding values would be both "Northings, Eastings".

Station Northings   Eastings
1   10001.00    10001.00
2   10070.09    10004.57
3   10105.80    10001.70

So far this is the only thing I have and it has this error when the function is called AttributeError: 'builtin_function_or_method' object has no attribute 'split'. Sorry I'm quite new to this.

def parsefile(file_name):
    station = []
    norths = []
    easts = []
    dict_station = {}
    with open(file_name) as fn:
        for line in fn:
            (stat,north,east) = line.split()
            stat.append(stat)
            norths.append(north)
            easts.append(east)
            dict_station[stat] = (north,east)
            print(station, norths, easts)
        
        return dict_station
rilke-nz
  • 25
  • 5

2 Answers2

0

The only error is that your trying to append to stat instead of station.

But your also doing a bunch of unnecessary stuff. This runs and is more efficient.

def parsefile(file_name):
    dict_station = {}
    with open(file_name) as fn:
        for line in fn:
            stat,north,east = line.split()
            dict_station[stat] = (north,east)
    return dict_station
jbflow
  • 384
  • 1
  • 11
0

As Station, Northings and Eastings have single value, instead of creating separate list for them try using below code here i am not considering first row to be stored as dictionary:

def parsefile(file_name):
    dict_station = {}
    with open(file_name) as fn:
        next(fn)
        for line in fn:
            temp = line.split()
            dict_station[temp[0]] = (temp[1],temp[2])
    return dict_station
Hetal Thaker
  • 556
  • 4
  • 9