Last active
September 27, 2015 04:08
-
-
Save 5263/1209263 to your computer and use it in GitHub Desktop.
Calc Checksums for ean (isbn13) and isbn10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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