Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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!
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 6,292 Feb-08-2020, 06:58 PM
Last Post: karkas
  What is wrong with this strings comparison minh 3 169 Jan-31-2020, 07:42 PM
Last Post: minh
  Iterating through a list of strings Ash_Ren 1 167 Nov-22-2019, 08:30 PM
Last Post: buran
  How do I delete symbols in a list of strings? Than999 1 195 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  Split a long string into other strings with no delimiters/characters krewlaz 4 193 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  convert strings of date to datetime exported from CSV GiorgosPap31 1 179 Oct-31-2019, 02:37 PM
Last Post: buran
  f-strings and backslashes newbieAuggie2019 5 451 Oct-26-2019, 06:58 AM
Last Post: buran
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 318 Oct-23-2019, 03:11 PM
Last Post: perfringo
  colouring strings newbieAuggie2019 23 1,297 Oct-23-2019, 08:59 AM
Last Post: newbieAuggie2019
  Back again on f-strings newbieAuggie2019 11 525 Oct-16-2019, 11:24 PM
Last Post: newbieAuggie2019

Forum Jump:


Users browsing this thread: 1 Guest(s)