Python Forum
convert a list of string+bytes into a list of strings (python 3)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert a list of string+bytes into a list of strings (python 3)
Hello everyone!

I would like to extract the first bytes of a binary file, and convert them into strings, in a list.
def binaryMethod(files, lenI):
outList = [] 
for _ in range(lenI):
            # read the length of the next string (read only the first 4 bytes) 
            blen = int(ceil(float(unpack('>I',[0]) / 4) * 4)
            # store the  string into outList
            outList.append(str(unpack('%ds' % blen,[0]).replace("\x00", ""))
where lenI is an integer.
files is the binary file.
This method works in Python 2 but not in Python 3 Huh

in Python 3, when I do
print (outList)

["b\'RT\\\\x00\\\\x00\'", "b\'RT\\\\x00\\\\x00\'", "b\'ts\\\\x00\\\\x00\'", "b\'MI\\\\x00\\\\x00\', ..."
While I am expecting something like: ["RT1", "RT2, ""ts1", "MI1", ...]

Thank you in advance for your help Smile
I forgot to mention, I tried to decode an element of the list, it did not work:
test = outList[0].decode('utf-8')
AttributeError: \'str\' object has no attribute \'decode\'
This doesn't make any sense.
First the format for a bytes string is zz = b'value'
and to decode, z = zz.decode('utf-8')
I have no idea what type of data "b\'RT\\\\x00\\\\x00\'" would be, certainly not bytes.
Yeah this is hexadecimal format
Python makes a clear distinction between bytes and strings . Bytes objects contain raw data — a sequence of octets — whereas strings are Unicode sequences . Conversion between these two types is explicit: you encode a string to get bytes, specifying an encoding (which defaults to UTF-8); and you decode bytes to get a string. Clients of these functions should be aware that such conversions may fail, and should consider how failures are handled.

We can convert bytes to string using bytes class decode() instance method, So you need to decode the bytes object to produce a string. In Python 3 , the default encoding is "utf-8" , so you can use directly:

b"python byte to string".decode("utf-8")

Possibly Related Threads…
Thread Author Replies Views Last Post
Sad ValueError: could not convert string to float badju 0 3,276 Jul-01-2021, 12:13 AM
Last Post: badju
  Join each list elements with string in other DF handy88 0 1,137 Feb-09-2021, 07:00 PM
Last Post: handy88
  Convert Strings to Floats samalombo 2 1,874 Jun-08-2020, 10:45 AM
Last Post: hussainmujtaba
  Indirectlty convert string to float in JSON file WBPYTHON 6 3,798 May-06-2020, 12:09 PM
  ValueError: could not convert string to float RahulSingh 3 3,115 Apr-09-2020, 02:59 PM
Last Post: dinesh
  Convert dataframe string column to numeric in Python darpInd 1 1,735 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  How to use list of symbols for sympy calculation in python? tanmaybhise 1 2,008 Mar-01-2020, 10:36 PM
Last Post: Gribouillis
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 4,461 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  TypeError: expected string or bytes-like object twinpiques 1 24,677 May-06-2019, 08:29 PM
Last Post: Yoriz
  Convert 'object' to 'string' AdWill97 1 60,537 May-06-2019, 08:22 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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