Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/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