Bottom Page

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  TypeError: expected string or bytes-like object twinpiques 1 71 May-06-2019, 08:29 PM
Last Post: Yoriz
  Convert 'object' to 'string' AdWill97 1 103 May-06-2019, 08:22 AM
Last Post: Yoriz
  List of colors adamG 1 147 Apr-08-2019, 06:45 PM
Last Post: Larz60+
  Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' mcgrim 3 138 Mar-22-2019, 01:29 PM
Last Post: buran
  Inserting data from python list into a pandas dataframe mahmoud899 0 188 Mar-02-2019, 04:07 AM
Last Post: mahmoud899
  convert images into pixel dataframe into csv file using python synthex 3 1,158 Feb-17-2019, 06:26 AM
Last Post: scidam
  Python List Comprehension. rinu 3 333 Jan-08-2019, 12:30 PM
Last Post: perfringo
  Creating matrix counting words in list of strings jazmad 2 355 Dec-23-2018, 05:47 PM
Last Post: jazmad
  Adding a string value to a dictionary that is inside a list mahmoud899 1 261 Dec-15-2018, 02:31 PM
Last Post: ichabod801
  Python dictionary adds only the last elements of a list mahmoud899 1 239 Dec-13-2018, 06:30 PM
Last Post: ichabod801

Forum Jump:

Users browsing this thread: 1 Guest(s)