1

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                  
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
LDF
  • 13
  • 2
  • It's not immediately apparent where the problem is (at least to me). Please make a [mre] with the [full error message with traceback](https://meta.stackoverflow.com/q/359146/4518341), an example of how you're calling the function, and try to remove any irrelevant code. FWIW, `filename` should be fine as either a `str` or `bytes` since `open()` accepts a [path-like object](https://docs.python.org/3/glossary.html#term-path-like-object) as its `file` parameter. – wjandrea Sep 14 '22 at 15:29
  • I think your question might be a duplicate. Take a look at: https://stackoverflow.com/a/5181085/7721752 – ukBaz Sep 15 '22 at 05:55

0 Answers0