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)
#1
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', files.read(4))[0]) / 4) * 4)
            # store the  string into outList
            outList.append(str(unpack('%ds' % blen, files.read(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)

Output:
["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
Reply
#2
I forgot to mention, I tried to decode an element of the list, it did not work:
test = outList[0].decode('utf-8')
Output:
AttributeError: \'str\' object has no attribute \'decode\'
Reply
#3
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.
Reply
#4
Yeah this is hexadecimal format
Reply
#5
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list of list with one or more empty one paul18fr 5 2,329 Oct-04-2023, 09:08 AM
Last Post: jeffreyteague
Sad ValueError: could not convert string to float badju 0 4,294 Jul-01-2021, 12:13 AM
Last Post: badju
  Join each list elements with string in other DF handy88 0 1,519 Feb-09-2021, 07:00 PM
Last Post: handy88
  Convert Strings to Floats samalombo 2 2,500 Jun-08-2020, 10:45 AM
Last Post: hussainmujtaba
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,830 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  ValueError: could not convert string to float RahulSingh 3 4,118 Apr-09-2020, 02:59 PM
Last Post: dinesh
  Convert dataframe string column to numeric in Python darpInd 1 2,271 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  How to use list of symbols for sympy calculation in python? tanmaybhise 1 2,762 Mar-01-2020, 10:36 PM
Last Post: Gribouillis
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 5,867 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  TypeError: expected string or bytes-like object twinpiques 1 25,677 May-06-2019, 08:29 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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