Python Forum
Deque to string in Python 3 - 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: Deque to string in Python 3 (/thread-27562.html)



Deque to string in Python 3 - anthares - Jun-11-2020

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


RE: Deque to string in Python 3 - buran - Jun-11-2020

(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



RE: Deque to string in Python 3 - anthares - Jun-11-2020

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)


RE: Deque to string in Python 3 - buran - Jun-11-2020

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.


RE: Deque to string in Python 3 - anthares - Jun-11-2020

all_data = b''.join(frames)

sorted out the problem...

thanks!