在 Apache NiFi (ExecuteStreamCommand) 中通过 python3 读取 excel
Reading excel via python3 in Apache NiFi (ExecuteStreamCommand)
我通过 HttpRequest(不是重点)收到一个 Excel 文件,并将其传递给 ExecuteStreamCommand 以获取特定单元格的值(通过 Python)。问题是我不知道如何从 sys.stdin 中准确获取 excel 以便通过 pandas 或 openpyxl.
获取值
import sys
print (type (sys.stdin))
在这样的构造中,类型
import sys
for line in sys.stdin:
print (type (line))
在此构造中,类型 (输出文件中有 387 行这样的行)
我刚刚开始理解这个话题,我几天前开始阅读 python 上的文章,几周前开始阅读 nifi 上的文章
在不知道您的数据是什么样子以及您实际想要实现什么的情况下,很难做到具体。
此博客 post 讨论了使用 ExecuteStreamCommand 处理标准输入 & python
https://mikethomsen.github.io/posts/2019/02/09/using-python-to-process-data-from-apache-nifi/
这个问题讨论的是使用 Python & pandas 从标准输入读取 xlsx
using pandas read_excel to read from stdin
我觉得这样做可能比这样做更复杂。
作为替代方案,您可以将 Excel sheet 写入文件,然后将文件位置传递给脚本以获取,例如
Http -> PutFile -> ExecuteProcess 或 ExecuteStreamCommand(文件位置作为参数)-> 流程的其余部分
这意味着您不必担心处理标准输入,因为您只是在处理文件
import sys
import pandas as pd
import io
bt = io.BytesIO(sys.stdin.buffer.read())
sheet = pd.read_excel(bt, "SheetName1", header=None)
res = sheet.at[1,2] #Cell 'C2'
print(res)
通过反复试验,找到了答案。
这段代码从 excel(sys.stdin)
中提取特定单元格的值
我通过 HttpRequest(不是重点)收到一个 Excel 文件,并将其传递给 ExecuteStreamCommand 以获取特定单元格的值(通过 Python)。问题是我不知道如何从 sys.stdin 中准确获取 excel 以便通过 pandas 或 openpyxl.
获取值 import sys
print (type (sys.stdin))
在这样的构造中,类型
import sys
for line in sys.stdin:
print (type (line))
在此构造中,类型
我刚刚开始理解这个话题,我几天前开始阅读 python 上的文章,几周前开始阅读 nifi 上的文章
在不知道您的数据是什么样子以及您实际想要实现什么的情况下,很难做到具体。
此博客 post 讨论了使用 ExecuteStreamCommand 处理标准输入 & python https://mikethomsen.github.io/posts/2019/02/09/using-python-to-process-data-from-apache-nifi/
这个问题讨论的是使用 Python & pandas 从标准输入读取 xlsx using pandas read_excel to read from stdin
我觉得这样做可能比这样做更复杂。
作为替代方案,您可以将 Excel sheet 写入文件,然后将文件位置传递给脚本以获取,例如
Http -> PutFile -> ExecuteProcess 或 ExecuteStreamCommand(文件位置作为参数)-> 流程的其余部分
这意味着您不必担心处理标准输入,因为您只是在处理文件
import sys
import pandas as pd
import io
bt = io.BytesIO(sys.stdin.buffer.read())
sheet = pd.read_excel(bt, "SheetName1", header=None)
res = sheet.at[1,2] #Cell 'C2'
print(res)
通过反复试验,找到了答案。 这段代码从 excel(sys.stdin)
中提取特定单元格的值