0

I have some file with bytes inside:

b'<html lang="en" class="wf-roboto-n4-active wf-roboto-n3-active wf-poppins-n7-active wf-poppins-n4-active wf-poppins-n3-active wf-roboto-n7-active wf-roboto-n5-active wf-sourcecodepro-n5-active wf-poppins-n5-active wf-poppins-n6-active wf-active"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">'

And i try to read this file inside unit tetst in 'rb' mode:

def test_04_foo(self):
    with open('test_data.txt', 'rb') as reader:
        print(reader.read())

    #some code here

And i have b'' in stdout.

How i can get bytes from file?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
MrOldSir
  • 616
  • 3
  • 9
  • 28

1 Answers1

1

The file contains a string representation of a bytes object, so you could solve this by following these questions: “safe” eval, Convert bytes to a string - something like this:

import ast

def test_04_foo(self):
    with open('test_data.txt', 'r') as reader:
        b = ast.literal_eval(reader.read())
    s = b.decode('utf-8')
    print(s)
wjandrea
  • 28,235
  • 9
  • 60
  • 81