Jeff Johnson wrote:

> This would mean scrolling through all of the keys to check to see if it 
> is in the description - and do it for each record.  Not very efficient 
> although it would work.
> 
> Might there be a better way?


I say, if it works fast enough in real life, stick with what you've got. 
  The only other thing I can think of would be to iterate the keys of 
the dict, and then the lines of the csv, changing them in place. IOW:

csv = [("account", "description"),
        ("blahSafewayBlah", "blah"),
        ("kkdkksksSafewayIII", "blah"),
        ("lksAlbertsonsdkfl", "blah")]

acct_dict = {"Safeway": "Groceries", "Albertsons": "Groceries"}

for k, v in acct_dict.items():
   for line in csv:
     if k in line[0]:
       line = (v, line[1])

# Now iterate your csv again to create the import file.


This will iterate your csv for every key in the dict, which would 
probably not be as good as iterating each line in the csv and then 
iterating each key, as you'll likely have more lines than keys in the dict.

This is really crying out for a database and selecting on 'description 
LIKE %(acct)%'. Something like:

"""
from sqlite3 import dbapi2 as sqlite

con = sqlite.connect(":memory:")
cur = con.cursor()
cur.execute("create table accounts (id integer primary key, account char")
cur.execute("create table descriptionspec (account_id integer, 
descriptionspec char")

cur.execute('insert into accounts (id, account) values (1, "Groceries")')
cur.execute("insert into accounts (id, account) values (2, "Auto")')
cur.execute("insert into descriptionspec (account_id, descriptionspec) 
values (1, '%%Safeway%%')")
cur.execute("insert into descriptionspec (account_id, descriptionspec) 
values (1, '%%Albertsons%%')")

for line in csv:
   desc = line[0]
   cur.execute("""
     select accounts.account
       from accounts
      inner join descriptionspec
         on descriptionspec.account_id = accounts.id
      where descriptionspec like '%(desc)s' """ % locals())
   rs = cur.fetchall()
   if rs:
     line[0] = rs[0][0]

... you get the idea

Now, what about cases where you don't always purchase the same types of 
things from the same place? For instance, we purchase prescription 
medicine *and* groceries from Nob Hill. Others purchase groceries *or* 
gasoline from Safeway....

Paul

-- 
http://paulmcnett.com


_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]

Reply via email to