Skip to main content
 首页 » 编程设计

python之nifi + executescript 使用 python 计算文件中的行数

2025年05月04日20cloudgamer

您好,我需要获取 csv 文件中不包括标题的第一行的行数 我需要将行数存储在一个属性中,并将未触及的流文件解析到下一个处理器

我正在考虑使用 extracttext,但我不认为正则表达式可以做到这一点。

所以下一步将是一个 executeScript 处理器。 我想到了一个带有以下模板的 python 脚本

flowFile = session.get()  
if (flowFile != None): 
# All processing code starts at this indent 
attrMap = ['numberOflines': '1', 'myAttr2': Integer.toString(2)] 
flowFile = session.get() 
if(!flowFile) return 
#Do something to get numbers of lines in the flow file 
i =0; 
    for line in flowfile 
        i+=1 
 
flowFile = session.putAttribute(flowFile, 'attribute_numberOfLines', i) 
if errorOccurred: 
    session.transfer(flowFile, REL_FAILURE) 
else: 
    session.transfer(flowFile, REL_SUCCESS) 

最后隐式返回

这不会运行

请您参考如下方法:

试试SplitText处理器的 Line Split Count 设置为比文件中可能的最大行数(例如 100 万)更高的某个数字。如果您想要总行数减去标题,您也可以将标题行数设置为 1。您将获得相同的流文件,但具有包含行数的属性 text.line.count。