Amidii
Amidii

Reputation: 365

Python - list search

How can i rewrite this program so i get all the data.txt to a list and so i can search after city and get time and local to that city using list objects and attributes?

Data.txt

City1
Time1
Local1
--------------
City2
Time2
Local2
--------------
City
Time
Local
--------------
City3
Time3
Local3
--------------

Program

class store:
    def __init__(self, city, time, local):
        self.city = city
        self.time = time
        self.local = local

    def readfile():
        row = "start"
        list = []
        infile = open("data.txt", "r", encoding="utf-8")
        while row != "":
            row = infile.readline()
            list.append(rad)
        infile.close()

store.readfile()

Upvotes: 0

Views: 330

Answers (4)

FMc
FMc

Reputation: 42421

The key idea: you want to process a file in sections, each having the same number of lines. So let's write a general-purpose method to do that:

def section_reader(file_name, n):
    # Takes a file name and an integer.
    # Will yield sections, each with N lines, until the file is exhausted.
    with open(file_name) as fh:
        while True:
            section = [fh.readline() for _ in xrange(n)]
            if len(section[0]): yield section
            else:               return

With that in place, the rest of the code is perfectly boring: a class definition, a utility method to clean up the lines in a section, etc.

class City(object):
    def __init__(self, name, time, local):
        self.name  = name
        self.local = local
        self.time  = time

def clean(section):
    return [line.strip() for line in section[0:3]]

cities = [ City(*clean(s)) for s in section_reader('data.txt', 4) ]

for c in cities:
    print [c.name, c.local, c.time]

Upvotes: 0

tamasgal
tamasgal

Reputation: 26329

class City(object):
    def __init__(self, name, time, local):
        self.name = name
        self.local = local
        self.time = time

class Store(object):
    def __init__(self):
        self.data = {}

    def readfile(self, filename):
        with open(filename, 'r') as datafile:
            subdata = []
            for line in datafile:
                if line.startswith('----'):
                    city = City(subdata[0], subdata[1], subdata[2])
                    self.data[subdata[0]] = city
                    subdata = []
                else:
                    subdata.append(line.rstrip())

    def city_named(self, city_name):
        return self.data[city_name]

store = Store()
store.readfile('Data.txt')

example_city = store.city_named('City1')

print(example_city.name)
print(example_city.time)
print(example_city.local)

Upvotes: 1

Mike Vella
Mike Vella

Reputation: 10585

If the file maintains that simple rigorous structure, this will do the trick:

def info_from_city(file,city):
    city += '\n'
    list = fh.readlines()
    city_index = list.index(city)
    time_index = list[city_index+1]
    local_index = list[city_index+2]

    return (time_index,local_index)

fh = open('data.txt')

print 'Time and local:'
print info_from_city(fh,'City2')

output is:

Time and local:
('Time2\n', 'Local2\n')

(note the newline chars - you may want to get rid of them using .replace('\n', ''))

the .index() method of a list returns the index of earliest instance of a particular string (any object or immutable type actually).

Upvotes: 0

Israel Unterman
Israel Unterman

Reputation: 13520

I would read the entire file and split it into strings like so:

with open('data.txt') as f:
    lst = f.read().split()

Then filter out the dash lines:

lst = [s for s in lst if not s.startswith('-')]

Then split the strings into groups of three, provided the number of strings is divisable by 3:

lst3 = [lst[i:i+3] for i in range(0, len(lst), 3)]

Finally assign the vars of your class:

for item in lst3:
    self.city, self.time, self.local = item

Upvotes: 1

Related Questions