简体   繁体   English

python:创建校验位 function

[英]python: create check digit function

I'm trying to create check digits and append them after the original UPCs.我正在尝试在原始 UPC 之后创建校验位和 append 它们。 Here's the sample data这是示例数据

Because there are leading 0's, I have to read the data as strings first:因为有前导 0,所以我必须先将数据作为字符串读取:
import pandas as pd upc = pd.read_csv("/Users/lee/Desktop/upc.csv", dtype = str)

Here's an example of the check digit algorithm:这是校验位算法的示例:
If upc is 003459409000如果upc是003459409000
step (1) 0 + 3*0 + 3 + 3*4 + 5 + 3*9 + 4 + 3*0 + 9 + 3*0 + 0 + 3*0 = 60步骤 (1) 0 + 3*0 + 3 + 3*4 + 5 + 3*9 + 4 + 3*0 + 9 + 3*0 + 0 + 3*0 = 60
step (2) 60 mod 10 = 0步骤 (2) 60 mod 10 = 0
step (3) check digit = 0 (if it's not 0, then check digit = 10 - number in step 2)步骤(3) check digit = 0 (if it's not 0, then check digit = 10 - number in step 2)

Based on the algorithm, here's the code:根据算法,代码如下:

def add_check_digit(upc_str):  
upc_str = str(upc_str)
if len(upc_str) != 12: 
raise Exception("Invalid length")

odd_sum = 0
even_sum = 0 
for i, char in enumerate(upc_str): 
j = i+1 
if j % 2 == 0: 
even_sum += int(char) 
else:m 
odd_sum += int(char) 
total_sum = (even_sum * 3) + odd_sum 
mod = total_sum % 10 
check_digit = 10 - mod 
if check_digit == 10: 
check_digit = 0 
return upc_str + str(check_digit) 

If I run this code, it gives correct check digit and appends this result to the end of the original UPC.如果我运行此代码,它会提供正确的校验位并将此结果附加到原始 UPC 的末尾。 For the example above, if I type:对于上面的示例,如果我键入:

add_check_digit('003459409000')

The output gives 13-digit UPC 0034594090000 . output 提供 13 位 UPC 0034594090000

Now my questions are:现在我的问题是:

  1. This function works only for a single upc, ie, I have to copy/paste each single upc and get the check digit.这个 function 仅适用于单个 upc,即,我必须复制/粘贴每个单个 upc 并获取校验位。 How do I create a function that works for a list of UPSs in a dataframe?如何创建适用于 dataframe 中的 UPS 列表的 function? Each result should return a 13-digit UPC with the check digits appended after the original UPC.每个结果应返回一个 13 位 UPC,并在原始 UPC 之后附加校验位。

  2. The UPCs are read as strings. UPC 被读取为字符串。 How do I apply the function to the UPCs?如何将 function 应用于 UPC? I suppose I should convert the strings to numbers somehow.我想我应该以某种方式将字符串转换为数字。 I'm really new to python.我对 python 真的很陌生。

  3. After I get the new UPCs, how do I save the result in a csv file?获得新的 UPC 后,如何将结果保存在 csv 文件中?

Thank you very much for your help.非常感谢您的帮助。

data set up for me as I don't have CSV file, below step is the same as your为我设置的数据,因为我没有 CSV 文件,以下步骤与您的相同

df = pd.read_csv("/Users/lee/Desktop/upc.csv", dtype = str)

data setup数据设置

import pandas as pd
df=pd.DataFrame({"upc_in_file":['003459409000','003459409001','003459409002']})
def add_check_digit(upc_str):  
    upc_str = str(upc_str)
    if len(upc_str) != 12: 
        raise Exception("Invalid length")

    odd_sum = 0
    even_sum = 0 
    for i, char in enumerate(upc_str): 
        j = i+1 
        if j % 2 == 0: 
            even_sum += int(char) 
        else:
            odd_sum += int(char) 
            total_sum = (even_sum * 3) + odd_sum 
            mod = total_sum % 10 
            check_digit = 10 - mod 
        if check_digit == 10: 
            check_digit = 0 
    return upc_str + str(check_digit) 

apply the above function to the upc column(the one which was read from file)将上述 function 应用于 upc 列(从文件中读取的列)

df['new_upc']=df['upc_in_file'].apply(add_check_digit)

now save the file!现在保存文件!

df.to_csv("my_updated_upc.csv")

this will look like这看起来像在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM