简体   繁体   English

如何在没有前缀'u'代码的情况下python Nifi ExecuteScript返回属性?

[英]How to Nifi ExecuteScript return attribute without prefix 'u' code python?

I have a json data incoming and I use ExecuteScript with Python code to extract key and value of this json data then put them into Attribute. 我有一个传入的json数据,我将ExecuteScriptPython代码一起使用以提取此json数据的键和值,然后将其放入Attribute中。 Here my code: 这是我的代码:

import json
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

class StreamCallback(StreamCallback):

    def __init__(self):
        pass

    def process(self, inputStream, outputStream):
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
        data = json.loads(text)
        for key in data:
            first = key
            break
        content = data[first]
        viewFlowFile = session.create()
        viewFlowFile = session.putAllAttributes(viewFlowFile,{'project': str(first), 'content': str(content)})
        session.transfer(viewFlowFile, REL_SUCCESS)
flowFile = session.get()
if flowFile != None:

    flowFile = session.write(flowFile, StreamCallback())
    session.transfer(flowFile, REL_FAILURE)
    session.commit()

When I run Job ExecuteScript returns string with prefix 'u'. 当我运行Job时, ExecuteScript返回带有前缀'u'的字符串。

My input: 我的输入:

{ "project_1": { "device_code": "V001", "line_code": "Anodiziing 12L"}} {“ project_1”:{“ device_code”:“ V001”,“ line_code”:“ Anodiziing 12L”}}

and the output of content Attribute: 以及内容属性的输出:

{ u'device_code': u'V001', u'line_code': u'Anodiziing 12L'} {u'device_code':u'V001',u'line_code':u'Anodiziing 12L'}

I also tried add 我也尝试添加

#!/usr/bin/python3

on header of body code. 在正文代码的标头上。 But there is no change. 但是没有变化。

My question is how to return string correctly without prefix 'u' using ExecuteScript Nifi? 我的问题是如何使用ExecuteScript Nifi正确返回不带前缀'u'的字符串?

Updated: We need convert type of data from dictionary to string. 更新:我们需要将数据类型从字典转换为字符串。 Using json.dumps(content) instead of str(content) and output will have no prefix 'u'. 使用json.dumps(content)代替str(content)和输出将没有前缀'u'。

JSON strings are Unicode. JSON字符串是Unicode。 The u'' is just an indicator that the dictionary you are printing contains Unicode strings. u''只是表明您正在打印的词典包含Unicode字符串。 If you don't want to see them, print the strings directly. 如果您不想看到它们,请直接打印字符串。 If you were running Python 3, it doesn't use u'' for Unicode strings, as they are the default, but you would see b'' for byte strings. 如果您运行的是Python 3,它将不使用u''作为Unicode字符串,因为它们是默认字符串,但是您会看到b''作为字节字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM