0

I already asked this before, but I made the question way too convoluted so I got answers that I can't use(this is for a uni project, so try not to import stuff). So, I have a list in a .txt like this:

num_cliene, store, name, surname, location

11, 2, LISA, ANDERSON, Evora

13, 2, KAREN, JACKSON, Lisbon

4, 2, BETTY, WHITE, Seixal

I need to access this data in a way that I can input a client number and it gives me their surname/location whatever, so far I was able to make a string

def listagerador():
clientesfich=open("clientes.txt", "r") 
listacli = ""
for line in clientesfich:
    listacli+=line

I can easily make it a tuple or list, but I fond string more convenient(but is it really?).

So, to summarize, how can I easily find some info given some info? Thanks.

Community
  • 1
  • 1
Henrique
  • 45
  • 4

3 Answers3

1

You can create a dict out of it, this way:

def listagerador(fname):
    d = {}
    with open(fname, "r") as clientesfich:
        for line in clientesfich:
            fields = line.rstrip().split()
            d[int(fields[0])] = fields[:1]
    return d

my_data = listagerador("clientes.txt")
client_num = 1
print my_data[client_num]
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
0

After you read this data from the text file, this seems like a good time to create a class. For example

class Client:
    def __init__(self, id_num, store_num, first_name, last_name, location):
        self.id_num = id_num
        self.store_num = store_num
        self.first_name = first_name
        self.last_name = last_name
        self.location = location
    def __repr__(self):
        return '{} {}'.format(self.first_name, self.last_name)

You could make an instance of a Client using whatever you read from file for each row

>>> lisa = Client(11, 2, 'Lisa', 'Anderson', 'Evora')
>>> karen = Client(13, 2, 'Karen', 'Jackson', 'Lisbon')
>>> betty = Client(4, 2, 'Betty', 'White', 'Seixal')

If you have a collection of these Client instances you can do filtering like checking a store number for example

>>> customers = [lisa, karen, betty]
>>> [client for client in customers if client.store_num == 2]
[Lisa Anderson, Karen Jackson, Betty White]

You could then create a dict of id_num to the instance of that Client, e.g.

>>> ids = dict((client.id_num, client) for client in customers)
>>> ids
{11: Lisa Anderson,
 4: Betty White,
 13: Karen Jackson}

Which lets you do lookups, then find any other info about them

>>> ids[4]
Betty White
>>> ids[4].location
'Seixal'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Note that I used `__repr__` in a way it was not intended for illustrative purposes, you can see what `__repr__` and `__str__` [should actually be used for here](https://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python). – Cory Kramer Dec 30 '15 at 17:28
0

For simple stuff, writing your own data-handling routines is fine. Once it starts getting a bit more complicated, you will save your sanity by delegating to a database.

Python has Sqlite3 built-in, and it's really straight-forward to use. First we need to stuff your data into the database. This only needs to be done once:

import sqlite3

# open (or create) the database file
db = sqlite3.connect('data/client_list')
cursor = db.cursor()

# create table
cursor.execute(
    """
    CREATE TABLE clients(
        id INTEGER PRIMARY KEY,
        num INTEGER,
        store_id INTEGER,
        name TEXT,
        surname TEXT,
        location TEXT
    )
    """
)
db.commit()

# load data from csv file
import csv
with open('clientes.txt', newline='') as inf:
    rd = csv.reader(inf, skipinitialspace=True)
    next(rd)    # skip header row
    data = list(rd)

# convert num and store_id from str to int
for row in data:
    row[0] = int(row[0])
    row[1] = int(row[1])

# stuff data into the database
cursor.executemany(
    """
    INSERT INTO clients(num, store_id, name, surname, location)
    VALUES(?, ?, ?, ?, ?)
    """,
    data
)
db.commit()

# make sure all changes get saved!
db.close()

Now we can query it like

import sqlite3

db = sqlite3.connect('data/client_list')
cursor = db.cursor()

def get_client_by_num(num):
    cursor.execute(
        """
        SELECT num, store_id, name, surname, location
        FROM clients
        WHERE num=?
        """,
        (num,)    # pass parameters as a tuple
    )
    return cursor.fetchone()

get_client_by_num(11)   # returns (11, 2, 'Lisa', 'Anderson', 'Evora')

which is overkill - but now that your data is in the database you can easily ask questions like "which 3 stores have the most customers?" or "how many customers did each store add in the past month?".

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99