You have many libraries to read Excel files. We can use openpyxl
next to collections.defaultdict
:
import collections
import openpyxl
wb = openpyxl.load_workbook('dat.xlsx')
ws = wb.get_sheet_by_name('Hoja1')
Lista = collections.defaultdict(list)
for row in ws.iter_rows():
Lista[row[0].value].append(row[1].value)
A itertools.defaultdict
object can be used exactly like a normal dictionary.
Another option is to use Pandas and take advantage of the pivot
If you are not going to operate with the data and you just want to get the dictionary this option is possibly too much for something so simple, however.
import pandas as pd
df = pd.read_excel("datos.xlsx", header = None).pivot(columns = 0, values = 1)
Lista = {col: df[col].dropna().tolist() for col in df.columns}
For a file like the following:
We get:
>>> Lista
{'connection A': ['auto', 'camioneta', 'puerta'],
'connection B': ['camioneta', 'puerta', 'capo']}
Note: Both pandas
and openpyxl
are external python libraries that need to be installed, via pip
for example.
Edit:
To store the variable in a file there are multiple forms. A very simple one is to serialize the object using cPickle / Pickle:
import collections
import pickle
import openpyxl
wb = openpyxl.load_workbook('dat.xlsx')
ws = wb.get_sheet_by_name('Hoja1')
Lista = collections.defaultdict(list)
for row in ws.iter_rows():
Lista[row[0].value].append(row[1].value)
# Ahora serializamos el objeto Lista.
# El archivo lo vamos a llamar 'Lista.dat':
with open("Lista.dat", "wb") as f:
pickle.dump(Lista, f)
Now we can from any module deserialize the object:
import pickle
with open("Lista.dat", "rb") as f:
Lista = pickle.load(f)
print(Lista)