Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print string after decode
#1
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
Reply
#2
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decode string ? JohnnyCoffee 1 785 Jan-11-2023, 12:29 AM
Last Post: bowlofred
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,890 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print first day of the week as string in date format MyerzzD 2 1,979 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  code decode, string, image ... teckow 2 2,025 Aug-20-2021, 07:02 PM
Last Post: teckow
  if a string has a digit - print tester_V 2 2,080 Jan-16-2021, 04:48 AM
Last Post: tester_V
  How to print string multiple times on new line ace19887 7 5,568 Sep-30-2020, 02:53 PM
Last Post: buran
  how to encode and decode same value absolut 2 2,279 Sep-08-2020, 09:46 AM
Last Post: TomToad
  Print a certain string only the first time it appears in a test file buttercup 5 2,697 Jul-23-2020, 01:30 PM
Last Post: palladium
  How to print string multiple times separated by delimiter Mekala 1 1,865 May-23-2020, 09:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020