0

I have results of script in text and bytes stored in CSV text file, which looks like:

found_value_1;b'UT\x05\x00\x03'

found_value_2;b'UT\x05\x00\x04'

There is some text and, separated by semicolon, dump of bytes. I was seriously looking, but could not find instructions or examples on how could I read this file's corresponding part as bytes. Manually I can copy b'UT\x05\x00\x03' and paste it to a variable like this:

found_value_1 = b'UT\x05\x00\x03'

and it is ready to go as type 'bytes'. It was simple to read this file and have 'u' type variables created with values like "b'UT\x05\x00\x03'" (probably having double-slashes), but as I tried converting it back to bytes nothing of available examples worked for me. Help is appreciated.

Community
  • 1
  • 1
Raul
  • 1
  • 1

1 Answers1

1

let's say you have your csv file. Read it and evaluate the bytes using ast.literal_eval:

import csv,ast.literal_eval
with open("input.csv") as f:
   cr = csv.reader(f,delimiter=";")
   for row in cr:
      print(ast.literal_eval(row[1]))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219