Skip to content

Instantly share code, notes, and snippets.

@5263
Last active September 27, 2015 04:08
Show Gist options
  • Save 5263/1209263 to your computer and use it in GitHub Desktop.
Save 5263/1209263 to your computer and use it in GitHub Desktop.
Calc Checksums for ean (isbn13) and isbn10
def isbn13t10(str1):
if len(str1)==13 and str1.startswith('978') and \
str1[12]==checksum(str1[0:12]):
return '%s%s' %(str1[3:12],isbnchecksum(str1[3:12]))
def isbn10t13(str1):
if len(str1)==10 and \
str1[9].upper()==isbnchecksum(str1[0:9]):
ean12='978%s'% str1[0:9]
return '%s%s' % (ean12,checksum(ean12))
def isbnchecksum(isbn9):
csum=0
for i in xrange(9):
csum += (i+1)*int(isbn9[i])
csum2 = csum % 11
if csum2 == 10:
return 'X'
else:
return '%d' % csum2
def checksum(sean):
"""calc EAN checksum"""
sum=0
for i,digit in enumerate(sean[::-1]):
sum+=int(digit)* (3 if (i & 1 ==0) else 1)
return str(10-(sum%10))[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment