Python Forum
lists, strings, and byte strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists, strings, and byte strings
#1
if i want to convert a string to a list then back, again, it's rather easy. if i want to do the same with a byte string it's not. anyone know a way that can be done to both using the same code for either?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Hm, let's try:

str.join('', list('Hello World'))
Output:
'Hello World'
Success :-)

Now the first try with bytes:
bytes.join(b'', list(b'Hello Bytes'))
Error:
TypeError: sequence item 0: expected a bytes-like object, str found
This is the problem with the representation. Iterating over bytes returns integers.

Third try:
bytearray(list(b'Hello Bytes'))
Output:
bytearray(b'Hello Bytes')
This looks better.

Finally you can use this:
bytes(bytearray(list(b'Hello Bytes')))
Output:
b'Hello Bytes'
But you're right. It's not so easy.

Bonus:
Instead of using the list function to construct a list,
use a bytearray. A bytearray is mutable and acts like a list.
bytes(bytearray(b'Hello Bytes'))
Output:
b'Hello Bytes'
It's mutable. You can mutate it.
ba = bytearray(b'Hello Bytes')
ba[0] = b'W'
Error:
TypeError: an integer is required
A not so nice solution for it:
ba[0] = ord(b'W')
ba
Output:
bytearray(b'Wello Bytes')
Extending:
ba.extend(b' This is the next Chunk')
ba
Output:
bytearray(b'Wello Bytes This is the next Chunk')
But i know what you mean. Handling with bytes a bit pain in the ass.
There must be a better way.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
right, iterating over bytes or a bytearray gets a list of ints. and doing so over a string gets a list of strings that are each len() == 1. either of these is easy enough to do and it is easy enough for the same code to do it. but reversing it back is harder, and doing it with the same code is even harder, still. it's the reversing step i want to do. one ambiguity is bytes vs bytearray in python3. given a list of ints, you don't know which to go back to. i'll accept bytearray because it is available in both python2 and python3. btw, the code i seek that can reverse a list back to a string or bytearray, i want it to be the same code working in both python2 and python3 ... python version agnostic code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Do regular expressions still need raw strings? bobmon 3 326 May-03-2024, 09:05 AM
Last Post: rishika24
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 405 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Tab Delimited Strings? johnywhy 7 650 Jan-13-2024, 10:34 PM
Last Post: sgrey
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 715 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  How to read module/class from list of strings? popular_dog 1 512 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Hard time trying to figure out the difference between two strings carecavoador 2 702 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Trying to understand strings and lists of strings Konstantin23 2 821 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 763 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Taking Mathematical Expressions from Strings quest 2 742 Jul-02-2023, 01:38 PM
Last Post: Pedroski55
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,552 Jun-09-2023, 08:42 PM
Last Post: ctrldan

Forum Jump:

User Panel Messages

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