Exporting panda data frame as excel file on FTP - excel

I am exporting a panda data frame as an excel file on FTP and using the below code. The code is creating a file on FTP. The issue here is that if I am make any change in the code and expecting a different output file it is creating the same output file as before. However if I change the file name in: myFTP.storbinary('STOR %s.xlsx' %filename,bio)..It works fine. Moreover, if I made the output on my local keeping the same name it also works fine. I dont want to change the file name every time I make some change in my code."It is not creating a different file with the same name" Below is the code:
myFTP = ftplib.FTP("ftp address","username","password)
myFTP.cwd("change directory/")
buffer=io.BytesIO()
df.to_excel(buffer,index=False)
text = buffer.getvalue()
bio = io.BytesIO(text)
file name = 'FileName_{0}{1}'.format(current_year,current_month)
myFTP.storbinary('STOR %s.xlsx'%file_name,bio)
myFTP.close()
Name of the output file must be: FileName_currentyearcurrentmonth

file name = 'FileName_{0}{1}'.format(current_year,current_month)
If this line of code is as it is in your code, well. It seens you have a syntax error. Also in cases like this contextual manager are actually pretty usefull. Why dont you try doing like this. So if you get an error well you dont keep your file open
with ftplib.FTP("ftp address","username","password) as myFTP:
myFTP.cwd("change directory/")
buffer=io.BytesIO()
df.to_excel(buffer,index=False)
text = buffer.getvalue()
bio = io.BytesIO(text)
file name = 'FileName_{0}{1}'.format(current_year,current_month)
myFTP.storbinary('STOR %s.xlsx'%file_name,bio)

Related

Getting error to read edf file using mne-python

I am trying to visualize the EEG data fie which is in .edf file format.For this purpose I am using MNE python.
here is my code
import mne
file = "/home/test.edf"
data = mne.io.read_raw_edf(file,preload=True)
Whenever I run this code below error massage is showing
ValueError: not enough values to unpack (expected 3, got 0)
I could not figure out where is my wrong.
It's not possible to use use the file by specifying the path, event if you add a "~", the directory won't be identified. Better you try to be in the exact directory and try reading the file, i.e go to your home directory and then try specifying the file.
import mne
file = "test.edf"
data = mne.io.read_raw_edf(file)

importing a .txt file into a .dat cplex opl

In an optimization problem I need to open my data for the problem in the .dat file. The problem is that the data is in a text file (notebook) and I don't know how I should connect it and work with a lot of instances. Could someone help me? I am searching for it, but I cannot find something that explains clearly.
Open your file in notepad
Go to Save As
Type yourfilename.dat (without space). You should put .dat extension manually
Save
You can read from a text file in cplex like:
execute //you should write this code in an execute block
{
var fileID= new IloOplInputFile("fileName.txt");
var contenct = fileID.readline();
var intContenct = Opl.intValue(contenct); //in case you want to convert what you read into an integer value
fileID.clos();
}

Run a VBA macro in Spotfire using Ironpython

So I would try ask over in this thread IronPython - Run an Excel Macro but I don't have enough reputation.
So roughly following the code given in the link I created some code which would save a file to a specific location, then open a workbook that exists there, calling the macro's within it, which would perform a small amount of manipulation on the data which I downloaded to .xls, to make it more presentable.
Now I've isolated the problem to this particular part of the code (below).
Spotfire normally is not that informative but it gives me very little to go on here. It seems to be something .NET related but that's about all I can tell.
The Error Message
Traceback (most recent call last): File
"Spotfire.Dxp.Application.ScriptSupport", line unknown, in
ExecuteForDebugging File "", line unknown, in
StandardError: Exception has been thrown by the target of an
invocation.
The Script
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.IO import File, Directory
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel
excel = Excel.ApplicationClass()
excel.Visible = True
excel.DisplayAlerts = False
workbook = ex.Workbooks.Open('myfilelocation')
excel.Run('OpenUp')
excel.Run('ActiveWorkbook')
excel.Run('DoStuff')
excel.Quit()
Right, so I'm answering my own question here but I hope it helps somebody. So the above code, as far as I'm aware was perfectly fine but didn't play well with the way my spotfire environment is configured. However, after going for a much more macro heavy approach I was able to find a solution.
On the spotfire end I've two input fields, one which gives the file path, the other the file name. Then there's a button to run the below script. These are joined together in the script but are crucially separate, as the file name needs to be inputted into a separate file, to be called by the main macro so that it can find the file location of the file.
Fundamentally I created three xml's to achieve this solution. The first was the main one which contained all of the main vba stuff. This would look into a folder in another xml which this script populates to find the save location of the file specified in an input field in spotfire. Finding the most recent file using a custom made function, it would run a simple series of conditional colour operations on the cell values in question.
This script populates two xml files and tells the main macro to run, the code in that macro automatically closing excel after it is done.
The third xml is for a series of colour rules, so user can custom define them depending on their dashboard. This gives it some flexibility for customisation. Not necessary but a potential request by some user so I decided to beat them to the punch.
Anyway, code is below.
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.IO import File, Directory
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel
from Spotfire.Dxp.Data.Export import *
from Spotfire.Dxp.Application.Visuals import *
from System.IO import *
from System.Diagnostics import Process
# Input field which takes the name of the file you want to save
name = Document.Properties['NameOfDocument']
# Document property that takes the path
location = Document.Properties['FileLocation']
# just to debug to make sure it parses correctly. Declaring this in the script
# parameters section will mean that the escape characters of "\" will render in a
# unusable way whereas doing it here doesn't. Took me a long time to figure that out.
print(location)
# Gives the file the correct extension.
# Couldn't risk leaving it up to the user.
newname = name + ".xls"
#Join the two strings together into a single file path
finalProduct = location + "\\" + newname
#initialises the writer and filtering schema
writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter)
filtering = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet()
# Writes to file
stream = File.OpenWrite(finalProduct)
# Here I created a seperate xls which would hold the file path. This
# file path would then be used by the invoked macro to find the correct folder.
names = []
for col in table.Columns:
names.append(col.Name)
writer.Write(stream, table, filtering, names)
stream.Close()
# Location of the macro. As this will be stored centrally
# it will not change so it's okay to hardcode it in.
runMacro = "File location\macro name.xls"
# uses System.Diagnostics to run the macro I declared. This will look in the folder I
# declared above in the second xls, auto run a function in vba to find the most
# up to date file
p = Process()
p.StartInfo.FileName = runMacro
p.Start()
Long story short: To run excel macro's from spotfire one solution is to use the system.dianostics method I use above and simply have your macro set to auto run.

How to save as a file but get its name at the same time?

I am currently using asksaveasfile to save the file, and it is working fine. The only issue I am having is that I cant get the new file name. I know asksaveasfilename exists but if I use them both together they will pop up two windows which I dont want to happen. If I try printing the output of asksaveasfile i get the following:
<_io.TextIOWrapper name='/home/work/newfile.txt' mode='w' encoding='UTF-8'>.
Is there a way I can just get the file name out of this?
Just use the .name attribute of the result of asksaveasfile:
import tkinter
file = tkinter.filedialog.asksaveasfile()
name = file.name
Or you can use asksaveasfilename then open the file yourself:
name = tkinter.filedialog.asksaveasfilename()
file = open(name,'w')
This way you could also use a with statement to ensure the file is closed properly which would be a good idea anyway:
name = tkinter.filedialog.asksaveasfilename()
with open(name,'w') as f:
NotImplemented #do stuff with the file

Upload and Save an excel file with BottlePy

I am creating an application using Bottle framework. I need a feature to upload an Excel file.
I am using the following for file upload.
http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads
On the server side I am getting the file data as binary content. I want to save it in a temporary folder as an Excel file.
I am new to Python and Bottle. Any help will be much appreciated.
Thanks
Chirdeep
Your request.files.data object contains the data about your excel file. So you only need to create a temporary folder and save it inside. This can be done using the tempfile module
f = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
f.write(request.files.data.file.read())
f.close()
I was not able to get simple file writing code like yours to work, So I used the tempfile module. Looking at your code, I would have assumed it would write to the directory where the python file is, if the code is working. Try using the code below, if you don't pass arguments to dir, it will create a file in the current directory.
def save_as_temp_file(data):
with tempfile.NamedTemporaryFile(dir=settings.TEMP_PATH,
delete=False,
suffix=".xlsx") as f:
f.write(data.file.read())
return f.name

Resources