Skip to main content
Question

Python Script - Overwriting records

  • June 6, 2019
  • 6 replies
  • 14 views

dataninja
Forum|alt.badge.img

 

name = feature.getAttribute('Chain')

query = {'Name': name}

params = {'query': json.dumps(query), 'fields': 'Name,Id', "OrderBy": "Name", 'limit': 5}

r = requests.get('https://location.chainxy.com/api/Chains', params=params, headers=headers)

 

for whatever in json.loads(r.content)['Records']:

<space> chainid = whatever['Id']

<space> chainname = whatever['Name']

<space> feature.setAttribute('CXY Name', chainname)

<space> feature.setAttribute('CXY Id', chainid)

------------------------------------------------------------------------------------------

Let's say that my list has three items and I expect my output to be as the following but apparently, my for loop is overwriting my record so I only end up with one record instead of three. How can I fix this problem?

------------------------------------------------------------------------------------------

Expected Output

Record ID --- CXY Name --- CXY Id

1 --- Hello --- 87

1 --- Hi --- 88

1 --- Nice --- 100

 

Actual Output

Record ID CXY Name CXY Id

1 Nice 100

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.

6 replies

daveatsafe
Safer
Forum|alt.badge.img+19
  • Safer
  • 1637 replies
  • June 6, 2019

Hi @trungn1993,

Do you want to create a new feature for each record, or do you you want to create list attributes on the feature?


dataninja
Forum|alt.badge.img
  • Author
  • 44 replies
  • June 6, 2019

Hi @trungn1993,

Do you want to create a new feature for each record, or do you you want to create list attributes on the feature?

How about both :)?


daveatsafe
Safer
Forum|alt.badge.img+19
  • Safer
  • 1637 replies
  • June 6, 2019

How about both :)?

Different processes for each.


dataninja
Forum|alt.badge.img
  • Author
  • 44 replies
  • June 6, 2019

Different processes for each.

I want to create list attributes on the feature.


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • June 6, 2019

This is the one way you can create a list on an attribute

import fme
import fmeobjects

def processFeature(feature):
    list = ['apple','banana','pear']
    for i, val in enumerate(list):
        feature.setAttribute('list{'+str(i)+'}.fruit',val)

Don't forget to enter the list attribute in the python caller under Attributes to Expose


david_r
Celebrity
  • 8394 replies
  • June 7, 2019

In addition to the reply by @egomm, here's a solution for creating one feature for each element in a Python list:

import fme
import fmeobjects
 
class processFeature(object):
    def input(self, feature):
        list = ['apple','banana','pear']
        for i, val in enumerate(list):
            new_feature = feature.clone()
            new_feature.setAttribute('list_value', val)
            self.pyoutput(new_feature)

This will create a copy of the incoming feature(s) for each element in the list, adding a new attribute "list_value".