Convert a bytes-like object

Viewed 29

This piece of my code worked in Python 2.7 but does it produces an error in Python 3.10 when I call the function. The error message is TypeError: cannot use a string pattern on a bytes-like object.

I think I just need to convert filename to a string but I can't seem to get the syntax right.

def parse(filename):
    i = 0
    
    with open(filename, 'rb') as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(), delimiters='\t,')
        csvfile.seek(0)
        reader = csv.reader(csvfile, dialect)

        print(reader)
                      
        for line in reader:
            
            x = info.append(line)
            print(x)
            if line[0] == 'SampleTestPlan':
                i = 1 
            if line[0] == 'AreaPerTest':
                    i = 0            
            if i >= 1:
                if i >= 2:
                    a = line[2:]                   
                    a = a[:2]
                    data.append(a)
                i = i + 1                  
0 Answers
Related