H
H
Hakka Madda2020-08-24 19:04:45
Python
Hakka Madda, 2020-08-24 19:04:45

TypeError: cannot use a string pattern on a bytes-like object?

Hello. Tell me why it doesn't work. When converted to a string, iteration is not performed ((

#FIXME: Not all bins are listed (not sure about Maestro)
    PAN_re = re.compile(r'''\b(?:(?P<visa>4\d{15})|'''
                            r'''(?P<mastercard>5[1-5]\d{14})|'''
                            r'''(?P<discover>6(?:011|5\d{2})\d{12})|'''
                            r'''(?P<amex>3[47]\d{13})|'''
                            r'''(?P<diners>3(?:0[0-5]|[68]\d)\d{11})|'''
                            r'''(?P<jcb>(?:2131|1800|35\d{3})\d{11}))\b''')

    ##TODO: Add full card track search:
    IATA_re = re.compile(r'''(?P<SS>%)(?P<FC>A|B|[C-M]|[N-Z])'''
                            r'''(?P<PAN>\d{13,19})\^(?P<NM>[\w \/\.]{2,26})\^'''
                            r'''(?P<ED>\d\d([0-2]\d|3[0-1]))(?P<SC>\d{3})'''
                            r'''(?P<PVV>\d{5})(?P<DD>\w+)(?P<ES>\?)(?P<LRC>\w)''')

    report = {}
    count = 0

    @classmethod
    def match(cls, data):
        """ Find matches in chunk of data """
        for m in cls.PAN_re.finditer(data):
            print("TEST")
            group = m.lastgroup
            pan = m.group(group)
            

            if is_valid(pan):
                cls.report.setdefault(cls.fp.name, [])
                cls.report[cls.fp.name].append(Card(pan, group, cls.masq))


TypeError: cannot use a string pattern on a bytes-like object

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-08-24
@bacon

In [1]: import re

In [2]: re.findall('\w', 'test')
Out[2]: ['t', 'e', 's', 't']

In [3]: re.findall('\w', b'test')

TypeError                                 Traceback (most recent call last)
<ipython-input-3-cc7df7bfccb0> in <module>
----> 1 re.findall('\w', b'test')

/usr/lib/python3.8/re.py in findall(pattern, string, flags)
    237 
    238     Empty matches are included in the result."""
--> 239     return _compile(pattern, flags).findall(string)
    240 
    241 def finditer(pattern, string, flags=0):

TypeError: cannot use a string pattern on a bytes-like object
would it be clearer?

P
PavelMos, 2020-08-24
@PavelMos

cannot use a string pattern on a bytes-like object
What data is in data ? Just in case, you need to look through type ().
The regexp string must be applied to text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question