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')

 

转载于:https://www.cnblogs.com/hltswd/p/5525419.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值