Anyone with experience parsing AIAG 2D Bar-codes with Python? Bar-code reader returns the string “[)>06P145013010Q60S000580431VELI003GT”, where ‘P’, ‘Q’, ‘S’,‘V’ are Field Identifers, not delimiters.
If they are always the same length you could just parse them out like
bc = “[)>06P145013010Q60S000580431VELI003GT”
p = bc[6:15]
q = bc[16:18]
s = bc[19:28]
v = bc[29:37]
If not you will have to get a bit fancier.
Thanks but unfortunately they are not the same length. i.e.
and each of these fields could change length
I’ll keep trying
According to the standards your scanner is supposed to embed codes inside the string that shows where the fields start and end. There should be a code FNC1 at the end of each fields values.
If your scanner is configurable for what it does at the end of each field, you could always have it put either a comma or a space at the end and then just split the returned string on that delimiter.
bc = “[)>06,P145013010,Q60,S000580431,VELI003GT”
ds = bc.split(",")
print ds[1:] #P
print ds[1:] #Q
print ds[1:] #S
print ds[1:] #V
You could work with python regular expressions: https://docs.python.org/2/library/re.html
import re regex = re.compile("P([0-9]*)Q([0-9]*)S([0-9]*)VELI([0-9]*)GT") regexResult = regex.search(barcodeString) if regexResult == None: print "no valid barcode" else: print regexResult.group(0) # the entire regex match print regexResult.group(1) # P print regexResult.group(2) # Q print regexResult.group(3) # S print regexResult.group(4) # VELI
Some information on regexes:
Regular expressions loop over the characters in a string, and check if they match. F.e.
P will match when the character
P is encountered. But you can also give a set of characters to the regex.
[0-9] will match any number.
Then there’s also repetition. The
* means it has to match the previous group 0 or more times. If you’d use a
+, it would have to match 1 or more times.
So the regex matches a P, followed by any numbers, followed by a Q, …
When you use parenthesis, it groups characters. F.e.
(abc)+ would match
abcabc etc. but not
aabbcc, the order is fixed in that group (unlike in a set in square brackets).
Then when you execute the regex, it will also return the groups to you. That’s why I put those number sets in parentheses, so it will extract those numbers.
@mwaynesmith We use these formats extensively. I’ll post our parsing library later today.