- 论坛徽章:
- 0
|
- #coding=utf-8
- '''
- * isbn操作类
- * 版权所有
- * @author t6760915<t6760915@gmail.com>
- * @version $Id: isbn.py $
- '''
- class isbn:
- #获取ISBN校验码
- def getIsbnCode(self, isbn):
- isbn = self.extractIsbn(isbn)
- if not isbn:
- return False
- if len(isbn) == 10:
- return self.getIsbn10Code(isbn)
- elif len(isbn) == 13:
- return self.getIsbn13Code(isbn)
- return False
- #计算10位ISBN的校验码
- def getIsbn10Code(self, isbn):
-
- isbn = self.extractIsbn(isbn)
-
- if not isbn:
- return False
- if len(isbn) != 10:
- return False
- intList = list(isbn)
- try:
- zipList = zip([int(i) for i in intList[0:9]], range(10, 1, -1))
- except:
- return False
- checksum = 11 - sum([i * j for i,j in zipList]) % 11
- if checksum == 10:
- return 'X'
- elif checksum == 11:
- return '0'
- return str(checksum)
- #计算13位ISBN的校验码
- def getIsbn13Code(self, isbn):
-
- isbn = self.extractIsbn(isbn)
- if not isbn:
- return False
- if len(isbn) != 13:
- return False
- intList = list(isbn)
- try:
- zipList = [(int(v[1]), self.getValueByIndex(v[0])) for v in enumerate(intList[0:12])]
- except:
- return False
- checksum = 10 - sum([i * j for i,j in zipList]) % 10
- if checksum == 10:
- return '0'
- return str(checksum)
- #10位ISBN转13位ISBN
- def isbn10to13(self, isbn):
-
- isbn = self.extractIsbn(isbn)
- if not isbn:
- return False
- if len(isbn) != 10:
- return False
-
- isbnList = list(str(isbn))
- newIsbn = '978' + ''.join(isbnList[0:9]) + self.getIsbnCode('978' + isbn)
- return newIsbn
- #13位ISBN转10位ISBN
- def isbn13to10(self, isbn):
-
- isbn = self.extractIsbn(isbn)
- if not isbn:
- return False
- if len(isbn) != 13:
- return False
- isbnList = list(str(isbn))
- newIsbn = ''.join(isbnList[3:12]) + self.getIsbnCode(''.join(isbnList[3:13]))
- return newIsbn
- #10位13位ISBN互转
- def convertIsbn(self, isbn):
- isbn = self.extractIsbn(isbn)
- if not isbn:
- return False
- if len(isbn) == 10:
- return self.isbn10to13(isbn)
- elif len(isbn) == 13:
- return self.isbn13to10(isbn)
-
- return False
- #检查是否为有效的ISBN
- def checkIsbn(self, isbn):
- isbn = self.extractIsbn(isbn)
- if not isbn:
- return False
- isbnList = list(str(isbn))
-
- if len(isbn) == 10 and isbnList[-1] == self.getIsbnCode(isbn):
- return True
- elif len(isbn) == 13 and isbnList[-1] == self.getIsbnCode(isbn):
- return True
-
- return False
- #获取字符串中由数字组成的子字符串
- def extractIsbn(self, isbn):
- isbnList = list(str(isbn).upper())
- sourceList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X']
- result = [i for i in isbnList if i in sourceList]
- if not result:
- return False
- try:
- result3 = ''.join(result[:3])
- except:
- result3 = ''
-
- #前缀把国家码改为:977(杂志),978,979,980
- if result3 in ['977', '978', '979', '980'] and len(result) == 13:
- return ''.join(result[0:13])
- elif len(result) == 10:
- return ''.join(result[0:10])
-
- return False
- #计算13位校验码时使用的工具函数
- def getValueByIndex(self, thisIndex):
- if thisIndex % 2 == 0:
- return 1
-
- return 3
- if __name__ == '__main__':
- isbnList = ['7-81090-021-8', '978-7-5603-2733-4', '7-5600-3879-4', '978-7-81090-021-8', '7119027468']
- isbnObj = isbn()
- for isbn in isbnList:
- #测试ISBN互转
- newIsbn = isbnObj.convertIsbn(isbn)
- #输出测试结果
- print '%s => %s' % (isbn, newIsbn)
复制代码
isbn.rar
(1.23 KB, 下载次数: 28)
|
|