zoukankan      html  css  js  c++  java
  • ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中

    # -*- coding: utf-8 -*-
    import math
    import re
    import csv
    import repr
    def ean_checksum(eancode):
        """returns the checksum of an ean string of length 13, returns -1 if the string has the wrong length"""
        if len(eancode) != 13:
            return -1
        oddsum=0
        evensum=0
        total=0
        eanvalue=eancode
        reversevalue = eanvalue[::-1]
        finalean=reversevalue[1:]
    
        for i in range(len(finalean)):
            if i % 2 == 0:
                oddsum += int(finalean[i])
            else:
                evensum += int(finalean[i])
        total=(oddsum * 3) + evensum
    
        check = int(10 - math.ceil(total % 10.0)) %10
        return check
    
    def sanitize_ean13(csvfile):
        reader = csv.reader(file(csvfile,'rb'))
        writer = csv.writer(file('C:/Users/Administrator/Desktop/1.csv','wb'))
        for id,ean13 in reader:
            if not ean13:
                return "0000000000000"
            ean13 = str(ean13)
            ean13 = '0' * (9-len(ean13)) + ean13
            ean13 = '042' + ean13
            ean13 = re.sub("[A-Za-z]","0",ean13);
            ean13 = re.sub("[^0-9]","",ean13);
            ean13 = ean13[:13]
            if len(ean13) < 13:
                ean13 = ean13 + '0' * (13-len(ean13))
            s=ean13[:-1] + str(ean_checksum(ean13))
            writer.writerow([id,s])
    sanitize_ean13('C:/Users/Administrator/Desktop/res.partner.csv')
  • 相关阅读:
    hibernate10--命名查询
    mybatis13--2级缓存
    mybatis12--一级缓存
    hibernate09--连接查询
    hibernate08--OpenSessionInView
    mybatis11--多对多关联查询
    mybatis10--自连接多对一查询
    mybatis09--自连接一对多查询
    mybatis08--关联查询多对一
    Oracle job启动与关闭
  • 原文地址:https://www.cnblogs.com/hltswd/p/5525419.html
Copyright © 2011-2022 走看看