假设我有一个由客户编号,商店编号,名字,姓氏和地址组成的列表,格式如下:
11, 2, Lisa, Anderson, NewYork
13, 4, John, Smith, Alabama
54, 2, Lucy, Nicholsson, NewYork
etc.
我在python中组织这些数据的最佳方式是什么,这样我就可以轻松访问它,这样我就可以做一些事情,比如输入一个客户编号,输出一个位置,以及其他类似的东西。
答案 0 :(得分:2)
您可以使用pandas。它提供类似数据库(或类似电子表格)的表,可用于存储和查询数据。像这样:
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'
但是,您可能还需要功能齐全的数据库(例如,请参阅SQLite)。
答案 1 :(得分:1)
如果您的数据相当一致并且没有那么多您想要一个成熟的数据库,那么您可以使用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')