0

Let's say I have a list composed of a client number, number of a store, first name, last name and address which in this format:

11, 2, Lisa, Anderson, NewYork

13, 4, John, Smith, Alabama

54, 2, Lucy, Nicholsson, NewYork

etc.

What is the best way for me to organize this data within python so I can easily access it so I can do stuff like input a client number and have an output with the location, and other derivatives of stuff like that.

Henrique
  • 45
  • 4
  • define a `class` with those attributes, and then create methods which will process and return the information you want. – dstudeba Dec 21 '15 at 17:08
  • I'm fairly sure the [Python tutorial](https://docs.python.org/3/tutorial/) goes over every option at your disposal. Did you have a specific problem understanding / applying that? – millimoose Dec 21 '15 at 17:19

2 Answers2

2

You can use pandas. It provides database-like (or spreadsheet-like) tables which can be used to store and query data. Like this:

import pandas as pd
df = pd.DataFrame([
        [11, 2, 'Lisa', 'Anderson', 'NewYork'],
        [13, 4, 'John', 'Smith', 'Alabama'],
        [54, 2, 'Lucy', 'Nicholsson', 'NewYork']
         ], columns = ['cl_number', 'store', 'first_name', 'last_name','address'])

df.index=df["cl_number"]
# rows will be indexed with cl_number

df.loc[11]
# returns a record of client with number 11

df.loc[11, 'address']
# returns address of a client with number 11

df[df['address'] == 'NewYork']
# returns a list of all clients with address 'NewYork'

However, you may also need full-featured database (see SQLite, for example).

Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78
1

If your data is reasonably consistent and there isn't so much that you want a fully-fledged database, you can get quite far with a namedtuple:

from collections import namedtuple

Client = namedtuple('Client', ('id', 'storeno', 'first_name', 'last_name',
                               'location'))

# Read the data
with open('db.csv') as fi:
    rows = fi.readlines()
db = {}
for row in rows:
    f= row.split(',')
    db[int(f[0])] = Client(int(f[0]), int(f[1]), f[2].strip(),
                           f[3].strip(), f[4].strip())

def find(**kwargs):
    """Return a list of all Clients matching a given set of criteria."""
    matches = []
    for client in db.values():
        for k,v in kwargs.items():
            if getattr(client, k) != v:
                break
        else:
            matches.append(client)
    return matches

# Client with ID 11
print db[11]

# All clients in New York
print find(location='NewYork')
# All clients called Lisa in New York
print find(location='NewYork', first_name='Lisa')
xnx
  • 24,509
  • 11
  • 70
  • 109