hi,
I read in excel file, do some transformations then run python.
How do I read in my input file into a line of python?
I should be more specific, I Want to take the output here for my input to python.
Hi @wonka1234
You can do this by adding the following code:
Import requests
dat = Alteryx.read("#1")
Alias = dat['Fieldname'].iloc[0]
As a breakdown, ("#1") is the incoming connection you want to read from, in your example there is only one but you could have multiple inputs and read in from several.
Alias can be whatever you want to call what you're reading in, for example File, Filepath etc - this will be the name you use to refer to later on.
dat['Fieldname'] has to be the name of the field from the incoming connection that you want to get the data from.
.iloc[0] means read from the start.
A fuller example from a blog post i did recently is below so you can see how it works in full:
from ayx import Package
from ayx import Alteryx
import requests
dat = Alteryx.read("#1")
Token = dat['Token'].iloc[0]
dat = Alteryx.read("#1")
UserID = dat['UserID'].iloc[0]
dat = Alteryx.read("#2")
URL = dat['URL'].iloc[0]
dat = Alteryx.read("#3")
Filepath = dat['Filepath'].iloc[0]
dat = Alteryx.read("#4")
Filename = dat['Filename'].iloc[0]
payload = {}
files = [
('file',(Filename, open(Filepath+Filename,'rb'),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))
]
headers = {
'UserID' : UserID,
'Token' : Token
}
response = requests.request("POST",URL,headers=headers,data=payload, files=files)
print(response.text)
So youre saying I should do this?
Import requests
dat = Alteryx.read("#1")
pl_Input_File= dat['Folder_Name'].iloc[0]
Hi @wonka1234
@yes, then you can reference the alias wherever you need it further on in your script.
Looks like I am getting an error, reading the data seems fine.
Sigh this python script runs fine in my spyder console.
SUCCESS: reading input data "#1"
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-3-6c54d0f324e1> in <module> 54 #%% Ensure all files/folders in the Script Config exist 55 for x in [pl_Input_File, pl_Base_Path, pl_Template_Dir]: ---> 56 if not x.exists(): 57 print('ERROR: does not exist:', x) 58 assert False AttributeError: 'str' object has no attribute 'exists'
May be barking up the wrong tree but in the code:
pl_Template_Dir = pl_Base_Path / '_FOLDER TEMPLATE' should you not be joining the pl_base_path to /'_folder_template' with a + sign?
@DavidSkaife , this is used to copy the contents of my FOLDER TEMPLATE into the newly created folders. Sorry it is confusing.