Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spreadsheet to python dictionary conversion

Tags:

python

ods

I am working on python and I want to read an *.ods file and convert it to a python dictionary.

The key will be the first column value and the value will be second column value.

How can I do it? I used xlrd but it does not read *.ods files.

like image 327
Lalit Chattar Avatar asked Jan 20 '11 08:01

Lalit Chattar


People also ask

How do I convert excel data to dictionary in Python?

First method Here we are going to use the pandas read_excel() method to read data from excel files and use the to_dict() method to convert data to dictionaries.

How do I read a XLSX file into a dictionary in Python?

So we use the read_excel data to read our excel file, by providing the name of the file, cities. xlsx , and the preceding ./ just indicates that the file is found in the current folder. Finally with the line travel_df. to_dict('records') we return a list of our dictionaries representing our data.

How do I convert a list to a dictionary in Python?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.


2 Answers

This approach from the link below works awesomely for me reading/loading *.ods files into python dataframe. You can choose to load by sheet index or by sheet name.

Peeped my solution from this project: https://pypi.org/project/pandas-ods-reader/

You might first need to install these dependencies: ezodf,lxml and pandas before continuing.

pip install pandas_ods_reader

from pandas_ods_reader import read_ods

Then:

filepath = "path/to/your/file.ods"

Doing loading of sheets based on indices (index 1 based)

sheet_idx = 1
df = read_ods(filepath, sheet_idx)

Doing loading of sheets based on sheet names

sheet_name = "sales_year_1"

df = read_ods(filepath, sheet_name)

Done.

like image 143
Laenka-Oss Avatar answered Oct 21 '22 09:10

Laenka-Oss


Some available options:

  • pyexcel-ods: "A wrapper library to read, manipulate and write data in ods format." Can be installed via: pip install pyexcel-ods. I personally recommend this package as I've used it and it is being actively maintained.

  • py-odftools: "... a collection of tools for analyzing, converting and creating files in the ISO standard OpenDocument format." This project hasn't been updated since late 2007. It looks abandoned.

  • ezodf: "A Python package to create/manipulate OpenDocumentFormat files." Installable via pip install ezodf. See caveat in the comments below about a serious issue with this package.

like image 27
Gabriel Avatar answered Oct 21 '22 09:10

Gabriel