Created
November 6, 2009 01:52
-
-
Save eight/227607 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""モジュラス10ウエイト3とISBNのチェックデジット計算。Python版。 | |
""" | |
def m10w31(code): | |
"""モジュラス10ウエイト3チェックデジットの計算 | |
""" | |
s = list(str(code)) | |
s.reverse() | |
sum = 0 | |
w = 3 | |
for i in s: | |
sum = sum + (int(i) * w) | |
if w == 3: w = 1 | |
else: w = 3 | |
d = sum % 10 | |
if d<>0: d = 10 - d | |
return str(d) | |
def isbn10to13(isbn10): | |
"""10桁ISBNを13桁ISBNに変換 | |
""" | |
s = '978'+isbn10[:9] | |
return s+m10w31(s) | |
def ssccBarCode(isbn13): | |
""" SSCC bar code | |
See, http://www.bisg.org/docs/shipping_label_guidelines_09-2005.pdf | |
""" | |
PRFX1='01' | |
PRFX2='1' | |
code = isbn13[:12] | |
return "(%s)%s %s-%s" % (PRFX1, PRFX2, code, m10w31(PRFX2+code)) | |
if __name__=='__main__': | |
data = ('4770025364','4770023230','4770015419','4881359290','4883731626','4873112109') | |
for isbn10 in data: | |
isbn13 = isbn10to13(isbn10) | |
print " %s -> %s -> %s" % (isbn10,isbn13,ssccBarCode(isbn13)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment