Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Creating Dictionary from excel data

Tags:

I want to create a dictionary from the values, i get from excel cells, My code is below,

wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value

and I want to create a dictionary, like below, that consists of the values coming from the excel cells;

{'class1': 1, 'class2': 3, 'class3': 4, 'classN':N}

Any idea on how I can create this dictionary?

like image 963
tuna Avatar asked Jan 07 '13 12:01

tuna


People also ask

How do you create a data dictionary in Python?

To create a Python dictionary, we pass a sequence of items (entries) inside curly braces {} and separate them using a comma ( , ). Each entry consists of a key and a value, also known as a key-value pair. Note: The values can belong to any data type and they can repeat, but the keys must remain unique.

Can you create a dictionary in Excel?

Click the Proofing tab and then click the Custom Dictionaries button. Excel opens the Custom Dictionaries dialog box where you can create a new custom dictionary.

How fetch data from Excel to Python?

Reading an Excel FileThe read_excel function of the pandas library is used read the content of an Excel file into the python environment as a pandas DataFrame. The function can read the files from the OS by using proper path to the file. By default, the function will read Sheet1.


1 Answers

or you can try pandas

from pandas import *
xls = ExcelFile('path_to_file.xls')
df = xls.parse(xls.sheet_names[0])
print df.to_dict()
like image 195
root Avatar answered Oct 27 '22 00:10

root