Hey you all
I am starting with python (more familiar with shell in general)

I am trying to write a script that replaces a column in a text file with numbers (or IDs) from an other file.
Let me rephrase it :

I have 2 files: ITEMS.txt and IDs.txt
Items.txt: (Location, Item)

Store1  Shampoo
Store2  Soap
Store3  Milk
Store4  Beer
Store5  Soap

IDs.txt : (ID, Item)

1  Beer
2  Shampoo
3  Soap
4  Milk

I am trying to replace the Item column in Items.txt with the corresponding IDs to get a new file
Final.txt (Location, ID)

Store1  2
Store2  3
Store3  4
Store4  1
Store5  3

Well, the files I got are bigger than those (2 Meg for Items.txt) and searching for each line of Items.txt every line of IDs.txt is quite annoying..

Any thoughts?
Thanks

Absolutely. You need ... dictionaries.

store_dict = {}
f1 = open("ITEMS.txt")
for line in f1:
   store, item = line.strip("/n").split(" ")
   store_dict[store] = item
f1.close()

items_dict = {}
f2 = open("IDs.txt")
for line in f2:
   item, ID = line.strip("\n").strip()
   items_dict[item] = ID
f2.close()

At this point, you now have all of the items indexed by store (in store_dict) and all of the IDs indexed by item (in item_dict), so...

ID_dict = {}
for store in store_dict:
   item = store_dict[store]
   if item in item_dict:
      ID_dict[store] = item_dict[item]
   else:
      ID_dict[store] = None

There's probably a schnazzier way to write the code, but that's clear enough, I hope.

Jeff

Sorry I couldn't make this work.
May be because it is little bit more complicated than this:

My items are not made of a single word :
STORES.txt

Store1  Shampoo Deluxe
Store2  Soap of Marseille
Store3  Milk from NE
Store4  Beer from Belgium
Store5  Soap of Marseille

I wanna have a list, not with numbers, but with first letters of each word, like this :
ITEMS.txt

BFB  Beer from NE
SD   Shampoo Deluxe
SOM Soap of Marseille
MFN  Milk from NE

(first, I couldn't write this part)

then, replace in STORES.txt, (or get a new file called NEW_STORES.txt), like this:

Store1  SD
Store2  SOM
Store3  MFN
Store4  BFN
Store5  SOM

Thanks a lot, I am stuck for a week now ..

The dictionary principle remains the same. It doesn't matter if there are multiple words or just one word.
dictionary key = what is in file = Store2 "Soap of Marseille" --> look up "Soap of Marseille"
so store_dic["Soap of Marseille"] = "SOM"
If you are unsure of punctuation, use isalpha to eliminate spaces, etc for both the key and the look up phrase.
To print the dictionary:
.for key in store_dic.keys():
......print store_dic[key], key --> "SOM Soap of Marseille"
If this doesn't explain it, post your code. It is more difficult to explain in the abstract.

Thanks a lot.. I finally understood the dictionary principle. It worked great.
But I am still stock at assigning the IDs part
I just started a new thread about that, so that I can close this one

thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.