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.
My code examples are always for Python >=3.6.0
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
  print function output wrong with strings. mposwal 5 347 Feb-12-2021, 09:04 AM
Last Post: DPaul
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 205 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  How to compare strings that have spaces hobbyist 2 271 Jan-14-2021, 12:44 PM
Last Post: perfringo
  read logfile between two specific strings FelixReiter 6 357 Jan-04-2021, 02:26 PM
Last Post: FelixReiter
  extract particular strings into a variable lokesh 3 307 Dec-09-2020, 11:59 AM
Last Post: lokesh
  Strange syntax error with f-strings Askic 6 658 Oct-16-2020, 10:40 AM
Last Post: Askic
  Convert file of hex strings to binary file medatib531 4 627 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  input inbetween two strings? KEYS 3 455 Sep-23-2020, 07:30 AM
Last Post: snippsat
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 1,537 Sep-18-2020, 10:10 PM
Last Post: tienttt
  Mutable Strings millpond 3 448 Aug-24-2020, 08:42 AM
Last Post: millpond

Forum Jump:

User Panel Messages

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