Skip to main content
Solved

how to pass a list from python


jelle
Contributor
Forum|alt.badge.img+16
  • Contributor


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)
View original
Did this help you find an answer to your question?

15 replies

jdh
Contributor
Forum|alt.badge.img+28
  • 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+28
  • Contributor
  • December 17, 2015

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


david_r
Evangelist
  • 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
Contributor
Forum|alt.badge.img+19
  • Contributor
  • 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+28
  • Contributor
  • December 18, 2015
david_r wrote:

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
Evangelist
  • December 21, 2015
jdh wrote:

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+28
  • Contributor
  • December 21, 2015
david_r wrote:

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+16
  • Author
  • Contributor
  • January 4, 2016
jdh wrote:

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+16
  • Author
  • Contributor
  • January 4, 2016
david_r wrote:

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+16
  • Author
  • Contributor
  • January 4, 2016
jdh wrote:

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+16
  • Author
  • Contributor
  • January 4, 2016
takashi wrote:

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
david_r wrote:

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
Evangelist
  • April 5, 2018
aquamarine wrote:
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
david_r wrote:
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
Evangelist
  • April 5, 2018
aquamarine wrote:
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!

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings