I have this text file in which there are certain products, each with the stores in which they are available. Store lines start with tab characters, product lines do not.
To be able to visualize it in a better way, I want to order it as a dictionary, having as a key the name of the store followed by a list of the products. An example is:
{
'Store1' : ['product1', 'product2'],
'Store2' : ...
}
This is an example of the data that I have, stores for each product:
- Crucial Ballistix BLT8G4D26BFT4K
- Infor-Ingen
- Bip
- PC Factory
- MyBox
- Patriot Signature Line PSD48G266681
- PC Express
- Soluservi
- Kingston KCP426NS6/8
- YouTech
- Bip
The expected output would have to be something like this (pretty printed):
{
'Infor-Ingen' : ['Crucial Ballistix BLT8G4D26BFT4K' ],
'Bip' : ['Crucial Ballistix BLT8G4D26BFT4K',
'Kingston KCP426NS6/8' ],
'PC Factory' : ['Crucial Ballistix BLT8G4D26BFT4K' ],
'MyBox' : ['Crucial Ballistix BLT8G4D26BFT4K' ],
'PC Express' : ['Patriot Signature Line PSD48G266681' ],
'Soluservi' : ['Patriot Signature Line PSD48G266681' ],
'YouTech' : ['Kingston KCP426NS6/8' ]
}
And I have this code
from collections import OrderedDict
od = OrderedDict()
tienda, producto ,otra,aux,wea= [], [],[], [],[]
with open("rams.txt","r") as f:
data = f.readlines()
for linea in data:
linea = linea.strip('\n')
if '\t' in linea:
tienda.append(linea.strip('\t'))
aux.append(linea.strip("\t").strip("\n"))
else:
otra.append(aux)
aux=[]
producto.append(linea)
aux.append(linea.strip("\n"))
tienda = sorted(list(set(tienda)))
for i in range(1,len(otra)):
wea=[]
for key in tienda:
if key in otra[i]:
wea.append(otra[i][0])
od[key] = wea
Now the problem is that, at the time of printing the dictionary, it gives me something like this:
('Bip', ['Crucial Ballistix BLT8G4D26BFT4K ']), ('Infor-Ingen', ['Crucial Ballistix BLT2K8G4D26BFT4K ']), ('MyBox', ['Crucial Ballistix CT16G4DFD8266']),..)