#-*-coding:utf-8-*-ImportMathImportReImportCSVImportreprdefean_checksum (eancode):"""returns the checksum of a EAN string of length, returns-1 if the string has the length of the wrong""" ifLen (eancode)! = 13: return-1Oddsum=0 Evensum=0 Total=0 Eanvalue=Eancode Reversevalue= Eanvalue[::-1] Finalean=reversevalue[1:] forIinchRange (len (Finalean)):ifI% 2 = =0:oddsum+=Int (finalean[i])Else: Evensum+=Int (finalean[i]) Total= (Oddsum * 3) +evensum Check= Int (10-math.ceil (total% 10.0))%10returnCheckdefsanitize_ean13 (csvfile): Reader= Csv.reader (File (CSVFile,'RB')) Writer= Csv.writer (File ('C:/users/administrator/desktop/1.csv','WB')) forId,ean13inchReader:if notEan13: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] ifLen (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')
Ean13 code generation, Python reads the data in CSV and processes the return and writes to another CSV file