• FME之PythonCaller遍历(循环)列表(数组)


    一、前言

            以前在FME软件遇到处理两组数据之间分别一一比对的时候就脑壳痛,FME大佬们都纷纷献计叫使用循环,然后我看了他们的模板,转换器太多,很绕,难以理解。后来他们又说可用Pythoncaller来写几行代码实现。因为之前没有系统学过Python语言,所以就此作罢。最近学了点Python,写了几个脚本,也有朋友问到这种问题,于是乎决定试下调用Pythoncaller来处理一下。

    二、任务要求

            当日期在某个开始时间和结束日期之间,那么就把图2的唯一值赋值给图1的待赋值。

     图1 需要根据日期区间来赋值的表格

     图2 需要作比对的表格

    三、实现路劲

    1. 用Aggregator转换器把 它们整成列表,当然可以用Listbuilder转换器
    2. 然后用FeatureMerger把它们合并成一个要素
    3. 进入PythonCaller转换器
    4. 把需要用到的列表传到转换器里来
    5. 创建过程列表
    6. 然后用一个for和while来执行一个列表与另一个列表元素之间的一一对比。
    7. 添加feature.setAttribute('df_list',df_list)代码,这个代码的意思就是把df_list列表设置为FME属性,但其实从PythonCaller转换器出来df_list其实还是列表。但你不能没有这句代码!
    8. PythonCaller要暴露的属性这里也一定要把输入df_list{},与上一步的设置FME属性代码缺一不可。
    9. 从PythonCaller出来后接一个ListExploder转换器把刚刚的df_list列表暴露出来即可。

            代码是需要卸载FeatureProcessor类的input函数里。我写的那部分代码有注释了开始和结束的标志,其他都是Pythoncaller自带的代码,不用管。代码如下:

    1. import fme
    2. import fmeobjects
    3. class FeatureProcessor(object):
    4. """Template Class Interface:
    5. When using this class, make sure its name is set as the value of the 'Class
    6. to Process Features' transformer parameter.
    7. """
    8. def __init__(self):
    9. """Base constructor for class members."""
    10. pass
    11. def input(self, feature):
    12. """This method is called for each FME Feature entering the
    13. PythonCaller. If knowledge of all input Features is not required for
    14. processing, then the processed Feature can be emitted from this method
    15. through self.pyoutput(). Otherwise, the input FME Feature should be
    16. cached to a list class member and processed in process_group() when
    17. 'Group by' attributes(s) are specified, or the close() method.
    18. :param fmeobjects.FMEFeature feature: FME Feature entering the
    19. transformer.
    20. """
    21. #新增代码开始
    22. print('分割线'+10*'——————')
    23. dg_list = feature.getAttribute('daiguajie{}.日期')
    24. ys_start_list = feature.getAttribute('yuanshi{}.开始日期')
    25. ys_end_list = feature.getAttribute('yuanshi{}.结束日期')
    26. id_list = feature.getAttribute('yuanshi{}.唯一值')
    27. df_list = []
    28. print(f'一共信息条数:{len(dg_list)}')
    29. for i in range(len(dg_list)):
    30. j = 0
    31. while j < len(ys_start_list):
    32. print(f'正在处理第{i}信息:{dg_list[i]}')
    33. print(f'开始和结束时间区间:{ys_start_list[j]}——{ys_end_list[j]}')
    34. if dg_list[i] >= ys_start_list[j] and dg_list[i] <= ys_end_list[j]:
    35. df_list.append(id_list[j])
    36. print(f'已匹配唯一值:{id_list[j]}')
    37. break
    38. else:
    39. j += 1
    40. if j < len(ys_start_list):
    41. print('不匹配')
    42. #当搜索到最后一个都没有比配到时,赋空值
    43. else:
    44. df_list.append('')
    45. print('未匹配到,赋空值')
    46. print('分割线'+10*'——————')
    47. #添加字段,此行必须要有!
    48. feature.setAttribute('df_list',df_list)
    49. #新增代码结束
    50. self.pyoutput(feature)
    51. def close(self):
    52. """This method is called once all the FME Features have been processed
    53. from input().
    54. """
    55. pass
    56. def process_group(self):
    57. """When 'Group By' attribute(s) are specified, this method is called
    58. once all the FME Features in a current group have been sent to input().
    59. FME Features sent to input() should generally be cached for group-by
    60. processing in this method when knowledge of all Features is required.
    61. The resulting Feature(s) from the group-by processing should be emitted
    62. through self.pyoutput().
    63. FME will continue calling input() a number of times followed
    64. by process_group() for each 'Group By' attribute, so this
    65. implementation should reset any class members for the next group.
    66. """
    67. pass

    五、任务小结

            PythonCaller转换器有个问题,就是进入的属性数据没有暴露的话,出来就会丢失,所以需要加个count等转换器来做个标识,方便重新挂接到原始数据上去。这只是测试用的,模板我已上传到资源,需要的朋友可以下载参考看下。 

  • 相关阅读:
    Python中的*args和**kwargs:打开函数的神秘大门
    伯俊ERP与金蝶云星空对接集成表头表体组合查询连通分布式调出单新增(调拨出库对接分布式调出(KD调拨)6月)
    Synchronized和Lock的区别
    mac 本地搭建go-admin
    从源码理解PyVarObject与PyObject的区别
    深度学习笔记(5)——pytorch实现Att-BiLSTM
    基础 | NIO - [Selector]
    “闭关修炼”,吃透这本Java核心知识,跳槽面试不心慌
    redis系列之——高可用(主从、哨兵)
    【WinForm详细教程八】WinForm中的TreeView控件
  • 原文地址:https://blog.csdn.net/seattle2009/article/details/126948523