Alert: There is a planned Community maintenance outage October 16th from approximately 10 - 11 PM PST. During this time the Alteryx Community will be inaccessible. Thank you for your understanding!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Read input data file into python tools input

wonka1234
10 - Fireball

hi,

 

I read in excel file, do some transformations then run python.

wonka1234_0-1668030327839.png

 

How do I read in my input file into a line of python?

wonka1234_1-1668030441353.png

 

10 REPLIES 10
wonka1234
10 - Fireball

I should be more specific, I Want to take the output here for my input to python.

 

wonka1234_0-1668030664890.png

 

DavidSkaife
13 - Pulsar

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)

 

wonka1234
10 - Fireball

@DavidSkaife  

 

So youre saying I should do this?

 

Import requests

dat = Alteryx.read("#1")
pl_Input_File= dat['Folder_Name'].iloc[0]

 

DavidSkaife
13 - Pulsar

Hi @wonka1234 

 

@yes, then you can reference the alias wherever you need it further on in your script. 

wonka1234
10 - Fireball

@DavidSkaife 

 

 

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'
DavidSkaife
13 - Pulsar

Hey @wonka1234 

 

Could you share the rest of the script, blanking out any confidential info?

wonka1234
10 - Fireball

@DavidSkaife 

 

 

DavidSkaife
13 - Pulsar

@wonka1234 

 

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?

wonka1234
10 - Fireball

@DavidSkaife  , this is used to copy the contents of my FOLDER TEMPLATE into the newly created folders. Sorry it is confusing.

Labels