Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created May 10, 2012 18:31
Show Gist options
  • Save doobeh/2654921 to your computer and use it in GitHub Desktop.
Save doobeh/2654921 to your computer and use it in GitHub Desktop.
Expand a UPC8 to EAN13 with Python
# expands a condensed UPC to an EAN. Expects the supplied upc to exclude the
# check-digit. Obviously you could test for length==6 and crop that check digit
# off if the barcode scanner can't be configured to supress it.
#
# Our Symbol PDT scanners send 'ssss' on the front of a scanned UPC, so this also
# strips that out, but it shouldn't cause any problems for a more standard barcode
# scanner.
upc = "0413924" # Kikkoman Soy Sauce
if len(str(int(upc)))==5:
upc = data.lstrip('s0')[:-1]
check = upc[5]
if check == '0':
upc = ''.join(['0', upc[0:2], '00000', upc[2:5]])
elif check == '1':
upc = ''.join(['0', upc[0:2], '10000', upc[2:5]])
elif check == '2':
upc = ''.join(['0', upc[0:2], '20000', upc[2:5]])
elif check == '3':
upc = ''.join(['0', upc[0:3], '00000', upc[3:5]])
elif check == '4':
upc = ''.join(['0', upc[0:4], '00000', upc[4]])
elif check == '5':
upc = ''.join(['0', upc[0:5], '00005'])
elif check == '6':
upc = ''.join(['0', upc[0:5], '00006'])
elif check == '7':
upc = ''.join(['0', upc[0:5], '00007'])
elif check == '8':
upc = ''.join(['0', upc[0:5], '00008'])
elif check == '9':
upc = ''.join(['0', upc[0:5], '00009'])
upc = str(int(upc)).zfill(13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment