I think the solution should be sought in the characters that could not be decoded. Is ti possible to find the specific characters?
Â
Â
Hopefully hope @sander can help us out, he seems to be the developer :-)
A small thing to test: modify line 113 of the PythonCaller to read:
prefix, suffix = pattern.decode('cp1252')
You may have to play around with the code page number
thanks for your reply @david_r but I'm not an expert on FME (self trained on it), so how can I modify Python code of that transformer ?
I think the solution should be sought in the characters that could not be decoded. Is ti possible to find the specific characters?
Â
Â
because of the error code "0xa0" it seems to be a white space
Â
Â
You need to right-click on the custom transformer and select Edit.
The custom transformer will open in a new instance of FME Workbench, where you can edit the PythonCaller. When saving the custom transformer, the easiest is to not change the transformer version number (you will be asked before saving).
You need to right-click on the custom transformer and select Edit.
The custom transformer will open in a new instance of FME Workbench, where you can edit the PythonCaller. When saving the custom transformer, the easiest is to not change the transformer version number (you will be asked before saving).
OK, it's done and now here are the errors :
Â
Â
Python Exception <AttributeError>: 'tuple' object has no attribute 'decode'
Traceback (most recent call last):
  File "<string>", line 30, in input
  File "<string>", line 113, in get_list_attribute_names
AttributeError: 'tuple' object has no attribute 'decode'
Error encountered while calling method `input'
f_42(PythonFactory): PythonFactory failed to process feature
A fatal error has occurred. Check the logfile above for details A fatal error has occurred. Check the logfile above for details
Â
Remote debugging is hard :-)
Try the following instead:
prefix, suffix = pattern
prefix = prefix.decode('cp1252')
suffix = suffix.decode('cp1252')
Remote debugging is hard :-)
Try the following instead:
prefix, suffix = pattern
prefix = prefix.decode('cp1252')
suffix = suffix.decode('cp1252')
Â
Yes, and I'm very sorry about that, how could I help ?
Â
Â
There's a new error now :
Â
Â
Traceback (most recent call last):
  File "<string>", line 30, in input
  File "<string>", line 120, in get_list_attribute_names
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 12: ordinal not in range(128)
Error encountered while calling method `input'
f_56(PythonFactory): PythonFactory failed to process feature
A fatal error has occurred. Check the logfile above for details
Error encountered while calling method `input'
Â
Â
Â
I'm trying to decode key_attr but didn't worked.
Â
Â
key_attr = feature.getAttribute('__key_attr'.decode('cp1252'))
If you are the author of the code I would recomend changing python interpreter to 3.6+. it is default to unicode characters.
If you are the author of the code I would recomend changing python interpreter to 3.6+. it is default to unicode characters.Â
and if you for some reason prefer to use python 2.7 take a look atÂ
http://python-future.org/imports.htmlÂ
you can add:
Â
from __future__ import unicode_literalsÂ
and if you did that one, you could do
Â
from __future__ import absolute_import, division, print_function, unicode_literals
which makes it compatible with python 3.
Â
Yes, and I'm very sorry about that, how could I help ?
Â
Â
There's a new error now :
Â
Â
Traceback (most recent call last):
  File "<string>", line 30, in input
  File "<string>", line 120, in get_list_attribute_names
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 12: ordinal not in range(128)
Error encountered while calling method `input'
f_56(PythonFactory): PythonFactory failed to process feature
A fatal error has occurred. Check the logfile above for details
Error encountered while calling method `input'
Â
Â
Â
I'm trying to decode key_attr but didn't worked.
Â
Â
key_attr = feature.getAttribute('__key_attr'.decode('cp1252'))
Ok, I found a solution, add line 120 this :  attr = attr.decode('cp1252')
Â
Â
It seems to work, I'll launch a complete test, before closing that post
Â
Â
Ok, I found a solution, add line 120 this :  attr = attr.decode('cp1252')
Â
Â
It seems to work, I'll launch a complete test, before closing that post
Â
Â
Great to hear that you found a solution.
Â
For reference, you could probably have done this as well:
Â
key_attr = feature.getAttribute('__key_attr').decode('cp1252')
Notice how it's the result from getAttribute() that you need to convert to unicode, not the attribute name.
If you are the author of the code I would recomend changing python interpreter to 3.6+. it is default to unicode characters.
I suspect that the custom transformer in question hasn't been tested with Python 3.x yet, and since the Python code is rather consequential I'm not sure how much work it would be.
Â
Hopefully @sander can help us out with a more permanent solution.
Â
Otherwise I agree completely.
I suspect that the custom transformer in question hasn't been tested with Python 3.x yet, and since the Python code is rather consequential I'm not sure how much work it would be.
Â
Hopefully @sander can help us out with a more permanent solution.
Â
Otherwise I agree completely.
yes, I can see it is allready written to be compatible with both. The he should just make sure his python environment is set to 3.x+
Â
Â
yes, I can see it is allready written to be compatible with both. The he should just make sure his python environment is set to 3.x+
Â
Â
Good catch!
Great to hear that you found a solution.
Â
For reference, you could probably have done this as well:
Â
key_attr = feature.getAttribute('__key_attr').decode('cp1252')
Notice how it's the result from getAttribute() that you need to convert to unicode, not the attribute name.
Ok, it seem the job doesn't keep the changes done on when I run it with a lot of lines, and use the basic one. How can I use the new transformer with the modifications ?
Â
Â
Ok, it seem the job doesn't keep the changes done on when I run it with a lot of lines, and use the basic one. How can I use the new transformer with the modifications ?
Â
Â
If possible, try simply skipping the modifications to the custom transformer and rather configure your main workspace to use Python 3.x:
Â
Â
(Thanks for tagging me, @david_r!)
Hi @nmeriotdev/Nicolas,
Sorry to hear about the trouble you are experiencing with "my" ListKeyValuePairExtractor!
Â
I did test it on some special characters/encodings, but with this stuff, it's hard to catch all cases...
Would you mind sending me (a part of) your workspace and some test data, so I can hopefully reproduce the issue and improve/fix the ListKeyValuePairExtractor? As I'm quite busy at the moment, I might need 1-2 weeks or so...
You could try David's suggestions, but I doubt if they will work. The decode() function converts a string into a unicode object, but the prefix and suffix objects are probably of type unicode already. Instead, I expect that the attr object should be decoded as a unicode object, so the startswith() and endswith() functions will be called on objects of the same type.
The first thing we could try, is to switch FME from Python 2.7 to 3.x (Workspace Parameters > Scripting > Python Compatibility). If you have 3.x installed and have the option to use it, that is. The advantage of Python 3.x, is that the unicode type no longer exists and that it always compares bytes objects.
If 3.x doesn't work for you, you could change the code as follows:
def get_list_attribute_names(self, feature, pattern):
    prefix, suffix = pattern
    attributes = feature.getAllAttributeNames()     # get all attribute names
    result_list = >]
    for attr in attributes:
        # filter out the attributes we need
        attr = attr.decode('cp1252')
        if attr.startswith(prefix) and attr.endswith(suffix):
            index = self.regex1.findall(attr)Â-1]   # gets the last number (= list index)
            result_list.append((index, attr))
    return sorted(result_list)
As David already mentioned, you might need to play with the exact encoding here.Â
Â
What does FME say in the log file around line 40? For instance, on my machine, it says:
Operating System: Microsoft Windows 10 64-bit  (Build 16299)
Â
FMEÂ Platform:Â WIN32
Â
Locale:Â en_GB
Â
Code Page: 1252  (ANSI - Latin I)
Alternatively, you can also change this line (from the code example above):
attr = attr.decode('cp1252')
into:
attr = attr.decode('cp1252', errors='ignore')
This will simply tell Python to ignore any bytes it cannot decode. Actually a bad solution, but at least your workspace will no longer crash here.
Â
Â
If this also doesn't work (i.e. the workspace still crashes), you could replace the get_list_attribute_names() function by this piece of code:
def get_list_attribute_names(self, feature, pattern):
    prefix, suffix = pattern
    attributes = feature.getAllAttributeNames()     # get all attribute names
    result_list = Â]
    for attr in attributes:
        # filter out the attributes we need
        try:
            if attr.startswith(prefix) and attr.endswith(suffix):
                index = self.regex1.findall(attr)p-1]   # gets the last number (= list index)
                result_list.append((index, attr))         except UnicodeError:           Â
            pass
    return sorted(result_list)
This is just a temporary solution though. I would love to properly fix the ListKeyValuePairExtractor...
Â
I hope you are able to share your (partial) workspace + data here.
I'm not sure if this gives the same result, but I gave it a try to make the whole thing a little bit shorter.
It will be easier to investigate if the result is close.
listkeyvalueextractor.fmx
That is exactly what I said in my response, but the answer I wrote (as always) was quite lengthy ;)
Â
Â
I'm not sure if this gives the same result, but I gave it a try to make the whole thing a little bit shorter.
It will be easier to investigate if the result is close.
listkeyvalueextractor.fmx
This version might work for you, @nmeriotdev. However, it will only work for basic lists (nice consecutive items) and it also doesn't work correctly for nested lists (lists inside lists). Hence the lengthy code...
Â
Please note that the transformers input fields might not work (anymore) as intended. You actually need to hack the parameters section in the FMX file to do so.
Has anyone had any luck fixing this? We've had a similar issue, hoping @sander might be able to help resolve.Â
The transformer does what we want it to and can get some features running through, but it causes the workspace to fail.Â
Here's an extract of the log file with the pertinent errors:Â
2019-12-31 13:57:24|   2.6|  0.1|WARN  |Python Exception <UnboundLocalError>: local variable 'error' referenced before assignment
2019-12-31 13:57:24|   2.6|  0.0|WARN  |Traceback (most recent call last):
  File "<string>", line 63, in input
UnboundLocalError: local variable 'error' referenced before assignment
2019-12-31 13:57:24|   2.6|  0.0|ERROR |Error encountered while calling method `input'
2019-12-31 13:57:24|   2.6|  0.0|FATAL |ListKeyValuePairExtractor_PythonCaller (PythonFactory): PythonFactory failed to process feature