Skip to main content
Best Answer

how to pass a list from python

  • December 17, 2015
  • 15 replies
  • 166 views

jelle
Contributor
Forum|alt.badge.img+22


Hi FME'ers


when creating a list with the following Python script:


import fme, os
import fmeobjects
folder = os.path.dirname(FME_MacroValues['Folder'])
def get_filepaths(feature):    """    This function will generate the file names in a directory     tree by walking the tree either top-down or bottom-up. For each     directory in the tree rooted at directory top (including top itself),     it yields a 3-tuple (dirpath, dirnames, filenames).    """
file_paths = []  # List which will store all of the full filepaths.    
# Walk the tree.
for root, directories, files in os.walk(folder):
        for filename in files:
            # Join the two strings in order to form the full filepath.
if '.dwg' in filename and '_georeferenced.dwg' not in filename:  
               filepath = os.path.join(root, filename)
               file_paths.append(filepath)  # Add it to the list.
feature.setAttribute('file_paths', file_paths)

I cannot easily retrieve the list of folders in FME.

I tried AttributeExposer with value 'file_paths{}', but it doesn't work

the AttributeExploder works and is a good workaround, but I'm curious how I could improve the script and expand my Python skills.  

Thanks in advance for your help.

best regards, 

Jelle

Best answer by jdh

replace the feature.setAttribute with the following

for i, path in enumerate(file_paths):
   feature.setAttribute('file_paths{'+str(i)+'}', path)
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

15 replies

jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • Best Answer
  • December 17, 2015

replace the feature.setAttribute with the following

for i, path in enumerate(file_paths):
   feature.setAttribute('file_paths{'+str(i)+'}', path)

jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • December 17, 2015

Why are you trying to do this in python rather than using the Directory and File Pathnames reader?


david_r
Celebrity
  • December 18, 2015

Here's an even shorter (easier?) solution:

feature.setAttribute('file_paths{}', file_paths)

Notice the curly brackets after the list name.

David


takashi
Celebrity
  • December 18, 2015

There always is more than one way :-) You can also add list elements to the feature while finding target file names, without storing the file paths into a Python list.

    i = 0
    for root, directories, files in os.walk(folder):
        for filename in [t for t in files if t.endswith('.dwg') and not t.endswith('_georeferenced.dwg')]:
            feature.setAttribute('file_paths{%d}' % i, os.path.join(root, filename))
            i += 1 

Scripting is fun, but I would firstly consider whether the PATH reader can be used, as @jdh mentioned at first.


jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • December 18, 2015

Here's an even shorter (easier?) solution:

feature.setAttribute('file_paths{}', file_paths)

Notice the curly brackets after the list name.

David

I've never been able to get that to work with non string values (which I grant are not required for the OP).  

 

Python Exception <TypeError>: Failure to convert list values to native values.  

Whereas the loop  version has no issue.


david_r
Celebrity
  • December 21, 2015

I've never been able to get that to work with non string values (which I grant are not required for the OP).

 

Python Exception <TypeError>: Failure to convert list values to native values.

Whereas the loop version has no issue.

Ah, I didn't know that was an issue, I guess I've never encountered that situation. Thanks for the info.


jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • December 21, 2015

Ah, I didn't know that was an issue, I guess I've never encountered that situation. Thanks for the info.

I mostly drop into python to do "list math", so encountered it very early on, and decided looping was a better way to go than type casting back and forth.


jelle
Contributor
Forum|alt.badge.img+22
  • Author
  • Contributor
  • January 4, 2016

Why are you trying to do this in python rather than using the Directory and File Pathnames reader?

thank you , I wasn't aware of this reader. Very good to know!


jelle
Contributor
Forum|alt.badge.img+22
  • Author
  • Contributor
  • January 4, 2016

Ah, I didn't know that was an issue, I guess I've never encountered that situation. Thanks for the info.

Hi David,

I remember trying this without success. It seemed obvious to me as well.


jelle
Contributor
Forum|alt.badge.img+22
  • Author
  • Contributor
  • January 4, 2016

replace the feature.setAttribute with the following

for i, path in enumerate(file_paths):
   feature.setAttribute('file_paths{'+str(i)+'}', path)

Thank you Joanna. I was very close, but you taught me the tiny bit I needed additionally. I really appreciate it!


jelle
Contributor
Forum|alt.badge.img+22
  • Author
  • Contributor
  • January 4, 2016

There always is more than one way :-) You can also add list elements to the feature while finding target file names, without storing the file paths into a Python list.

    i = 0
    for root, directories, files in os.walk(folder):
        for filename in [t for t in files if t.endswith('.dwg') and not t.endswith('_georeferenced.dwg')]:
            feature.setAttribute('file_paths{%d}' % i, os.path.join(root, filename))
            i += 1 

Scripting is fun, but I would firstly consider whether the PATH reader can be used, as @jdh mentioned at first.

Thank you Takashi! Again, I learned something from you. 


Forum|alt.badge.img+1

Here's an even shorter (easier?) solution:

feature.setAttribute('file_paths{}', file_paths)

Notice the curly brackets after the list name.

David

Hi @david_r

 

I regularly use PythonCaller for dealing with lists , and I always get into trouble when I have a list that contains non-string items. I end up having to deal with them as strings within PythonCaller otherwise I get the same error as @jdh 'Python Exception : Failure to convert list values to native values.'

 

Do you know if there are any plans to fix this in future releases of FME (I am using FME 2017.1.1.1).

 

Thanks,

 


david_r
Celebrity
  • April 5, 2018
Hi @david_r

 

I regularly use PythonCaller for dealing with lists , and I always get into trouble when I have a list that contains non-string items. I end up having to deal with them as strings within PythonCaller otherwise I get the same error as @jdh 'Python Exception : Failure to convert list values to native values.'

 

Do you know if there are any plans to fix this in future releases of FME (I am using FME 2017.1.1.1).

 

Thanks,

 

Unfortunately I have no such insider information... :-(

 

If you need help finding a workaround, feel free to post it as a new questions, though. I'm sure someone has already done something on this subject.

Forum|alt.badge.img+1
Unfortunately I have no such insider information... :-(

 

If you need help finding a workaround, feel free to post it as a new questions, though. I'm sure someone has already done something on this subject.
No worries @david_r. I have a work around so it's not a major issue for me right now, but I have just posted an idea so Safe might consider changing it in future releases.

 

https://knowledge.safe.com/idea/67564/enable-pythoncaller-to-deal-with-lists-that-contai.html?

 


david_r
Celebrity
  • April 5, 2018
No worries @david_r. I have a work around so it's not a major issue for me right now, but I have just posted an idea so Safe might consider changing it in future releases.

 

https://knowledge.safe.com/idea/67564/enable-pythoncaller-to-deal-with-lists-that-contai.html?

 

Excellent, upvoted!