Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving list of many python variables into excel sheet while simultaneously keeping variable types defined?

It's possible with xlsxwriter to save variables to existing excel files and read them after, though the problem is that the variables are stored as strings in my excel file.

Let's say I have a list of many different variables with various types (pd.datetimerange, pd.df, np.arrays, etc.), if I save them to the excel file the variable type would be lost.

Also the Python script is called from my Excel file so I can't change anything in it without writing a VBA script. Which would temporarily close my workbook, dump the chars (with say pickle strings) and reopen it.

Is it possible to retrieve the types from the excel file without writing a parsing function first (which would take the excel string and yield me with the equivalent variable type)?

like image 684
Bython Avatar asked Dec 07 '18 05:12

Bython


People also ask

How do you manipulate data in Excel using Python?

Openpyxl is a Python library that provides various methods to interact with Excel Files using Python. It allows operations like reading, writing, arithmetic operations, plotting graphs, etc. This module does not come in-built with Python. To install this type the below command in the terminal.

Can Python interact with Excel?

Pandas is the most widely used Python library to interact with Excel files. You can use this library to load data from your Excel spreadsheet and perform key operations like inserting, deleting columns and rows, and appending information if necessary.

How do you write multiple rows in Excel in Python?

To insert multiple rows into the worksheet, call the insertRows method of the Cells collection. The InsertRows method takes two parameters: Row index, the index of the row from where the new rows will be inserted. Number of rows, total number of rows that need to be inserted.


1 Answers

As per your comment, you can get eval to correctly process the symbols that are local to some module by passing the appropriate dict of locals into eval, along with your string. Here's a workable solution:

import pandas as pd

def getlocals(obj, lcls=None):
    if lcls is None: lcls = dict(locals().items())

    objlcls = {k:v for k,v in obj.__dict__.items() if not k.startswith('_')}
    lcls.update(objlcls)

    return lcls

x = "[123,DatetimeIndex(['2018-12-04','2018-12-05', '2018-12-06'],dtype='datetime64[ns]', freq='D')]"
lcls = getlocals(pd)

result = eval(x, globals(), lcls)
print(result)

Output:

[123, DatetimeIndex(['2018-12-04', '2018-12-05', '2018-12-06'], dtype='datetime64[ns]', freq='D')]

As a Responsible Person, it is also my duty to warn you that using eval for your application is ridiculously unsafe. There are many discussions of the dangers of eval, and none of them suggest there's a way to completely mitigate those dangers. Be careful if you choose to use this code.

like image 83
tel Avatar answered Oct 29 '22 16:10

tel