Python Forum
Print string after decode - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Print string after decode (/thread-23485.html)



Print string after decode - martinzeifang - Jan-01-2020

Hallo,

I have a small programm. In a function I receive date an decode it back to utf-8
the function returns the data:

def reliable_recv():
    data = ""
    while True: 
        try:
            data = data + target.recv(1024).decode('utf-8')
            return json.loads(data)
        except ValueError:
            continue
if I print the data it's printed like that:

b'this ist the text\nthis is more text'

If I try to

result = reliable_recv()
print(result.decode()) 
I get the error
Error:
AttributeError: 'str' object has no attribute 'decode'
so my question:
How can I print my data?

Thank you for your help

Martin


RE: Print string after decode - buran - Jan-02-2020

(Jan-01-2020, 10:04 PM)martinzeifang Wrote: if I print the data it's printed like that:
where do you print data. this is not shown at all.

what you show is trying to use .decode() on result (i.e. what is returned by the function). but that is after json.loads(data) that will produce str.