Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deque to string in Python 3
#1
Hi everyone,

Fairly new to Python here and dealing with some legacy code.

n Python2 I was able to do something similar to:

frames = deque(maxlen=xyz)
framesString = ''.join(frames)
In Python3 I get an error.

How should I change it in order to get a string representing the deque object?

Thanks in advance,
G
Reply
#2
(Jun-11-2020, 05:07 AM)anthares Wrote: In Python3 I get an error.
what error do you get? Post full traceback in error tags
Please provide example of the data in the deque.

from collections import deque

foo = deque(['a', 'b', 'c'])
print(''.join(foo))
Output:
abc
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
#3
TypeError: sequence item 0: expected str instance, bytes found


the actual code is:
frames = deque(maxlen=self.previous_audio_seconds * chunks_per_second)

...
# frames is filled in with a pyaudio stream
...

all_data = ''.join(frames)
Reply
#4
You've been asked to post full traceback and also to use BBcode. Please, do so in the future.

The error is clear - you have bytes, not str. convert bytes before pass frames to ''.join(). the error has nothing to do with deque.
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
#5
all_data = b''.join(frames)

sorted out the problem...

thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If you deque a list, can it still be indexed? netrate 6 10,110 Nov-07-2016, 12:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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