ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中
2016-05-24 23:29
1016 查看
# -*- 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')
相关文章推荐
- python 破解wp博客后台登陆密码
- python 爬虫(二) 爬虫知乎 制作成电子书发送到kindle
- python数据持久存储:pickle模块的使用
- 让你瞬间萌比的35个python小技巧
- 文成小盆友python-num3 集合,函数,-- 部分内置函数
- 深入浅出Python装饰器
- python读文件
- Numpy文件存取
- Python -- 文件 目录
- Selenium学习(Python)
- python读写操作文件
- Python学习笔记(二)在线用pip下载第三方包
- Python实现各种排序算法
- Python 2.7.x 和 3.x 版本的重要区别
- python 2.7中安装mysql
- python 2.7中安装mysql
- python 2.7中安装mysql
- Python之时间处理模块time
- Python -- 文件/目录 方法
- Python爬虫实战一之爬取糗事百科段子