Shandeep Murugasamy
Shandeep Murugasamy

Reputation: 311

How to remove "u" in output. Error: AttributeError: 'dict' object has no attribute 'encode'

I am trying to store JSON output in a variable. Initially I got the output but it was prepended with character "u" (unicode). Below is the initial output:

{u'imdata': [{u'nvoNws': {u'attributes': {u'dn': u'sys/epId-1/nws', u'status': u'', u'persistentOnReload': u'true', u'monPolDn': u'', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'uid': u'0', u'childAction': u''}}}, {u'nvoPeers': {u'attributes': {u'dn': u'sys/epId-1/peers', u'status': u'', u'persistentOnReload': u'true', u'monPolDn': u'', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'uid': u'0', u'childAction': u''}}}, {u'nvoEp': {u'attributes': {u'status': u'', u'operState': u'down', u'persistentOnReload': u'true', u'propFaultBitmap': u'', u'hostReach': u'0', u'adminSt': u'disabled', u'holdUpTime': u'0', u'encapType': u'0', u'uid': u'0', u'epId': u'1', u'sourceInterface': u'unspecified', u'descr': u'', u'monPolDn': u'uni/fabric/monfab-default', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'holdDownTimerExpiryTime': u'NA', u'autoRemapReplicationServers': u'no', u'operEncapType': u'0', u'dn': u'sys/epId-1', u'mac': u'00:00:00:00:00:00', u'cfgSrc': u'0', u'childAction': u'', u'vpcVIPNotified': u'no', u'learningMode': u'0', u'controllerId': u'0', u'holdUpTimerExpiryTime': u'NA', u'holdDownTime': u'180'}, u'children': [{u'nvoPeers': {u'attributes': {u'status': u'', u'persistentOnReload': u'true', u'monPolDn': u'', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'uid': u'0', u'rn': u'peers', u'childAction': u''}}}, {u'nvoNws': {u'attributes': {u'status': u'', u'persistentOnReload': u'true', u'monPolDn': u'', u'modTs': u'2017-02-24T00:50:47.373+00:00', u'uid': u'0', u'rn': u'nws', u'childAction': u''}}}]}}], u'totalCount': u'3'}

So then I added encode('utf-8') to my print output statement and after that I got below error:

Traceback (most recent call last):
  File "/Users/shandeep/Desktop/n9k-rest/Andys_payloads/Andys_script.py", line 548, in <module>
    get_interface_nve()
  File "/Users/shandeep/Desktop/n9k-rest/Andys_payloads/Andys_script.py", line 113, in get_interface_nve
    print(x.encode('utf-8'))
  AttributeError: 'dict' object has no attribute 'encode'

Below is definition and function call.

def request_get(dn):
    cookie = login_api()
    response = requests.get(url + dn + '?query-target=subtree&rsp-subtree=full', cookies=cookie, verify=False)
    print('Valid response: \n' + response.text)
    return response.json()

def get_interface_nve():
    x = request_get('/api/mo/sys/epId-1.json')
    print('PRINT OUTPUT: \n')
    #print(x)
    print(x.encode('utf-8'))

Function call:

get_interface_nve()

Upvotes: 0

Views: 35461

Answers (1)

dsh
dsh

Reputation: 12214

dict objects do not have a method encode(). That is a method for str objects. The text you see is python's "repr" representation of a (unicode) string. You only have it because you used the wrong approach to convert your dict to a string.

You need to convert your dict to JSON. This is not achieved using print() or repr() or str(). Use the json module for this.

Eg:

x = {u'imdata': [{u'nvoNws': {u'attributes': {u'dn': u'sys/epId-1/nws'}}}]}
json.dump(x, sys.stdout)

Upvotes: 11

Related Questions