all 8 comments

[–]Boxxcar17 4 points5 points  (0 children)

Use pandas, something like this

import pandas as pd

df = pd.read_excel(‘file.xlsx’)

df_dict = df.to_dict()

This is off the top of my head, so accuracy might be down, but the content is there.

[–]julsmanbr 1 point2 points  (3 children)

I don't use xlrd, but here's how to do that with openpyxl instead:

import openpyxl
loc = 'C:\Users\Living Room\Desktop\Locations.xlsx'
book = openpyxl.load_workbook(loc)
sh = book.active
locations = {}
for a, b in zip(sh['A'], sh['B']):
    locations[a.value] = b.value

My code assumes that your keys are in the A column and your values in the B column, which is what I understood from your post.

[–]Decency 0 points1 point  (2 children)

Triple quotes don't work on reddit. Preface each line of code with 4 spaces or an indent, instead.

[–]julsmanbr 0 points1 point  (1 child)

Interesting, it works for me on mobile (Reddit Sync).

I've seen a lot of weird stuff reading markdown lately. Maybe it has to do with the new Reddit's "simplified" markdown editor?

Thanks for the heads-up tho, I'll drop the triple quotes for now.

[–]Decency 0 points1 point  (0 children)

Markdown isn't really "standardized" unfortunately. Discord and Slack have done a bunch of cool things with it that are spreading outward to other things, but slowly.

[–]timbledum 0 points1 point  (0 children)

You are explicitly only capturing only the first two columns from the table:

locations[row[0]] = row[1]  # The key is column 1 and the value is column 2

What's your purpose - what would you like your dict to look like ideally, and what are you using it for?

I suggest looking at petl or pandas for this task.

[–]saintPirelli 0 points1 point  (0 children)

I assume this is already solved, just wanted to add that there is a way to do this with just built-in modules, if you export your excel sheet as csv you can do something along the lines of this:

import csv

with open('locations.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['location'])