python 代码中的错误输出用于搜索和计算 excel 中扫描的条形码

wrong output in a python code for search and count the scanned barcodes in excel

我有一个 xlsx 文件,它的“C”列包含事物的条形码,我想插入条形码来计算它们(每个条形码的数量将放在“G”列的相应单元格中) 主要问题是: 当插入的条形码在“C”列的任何单元格中都不存在时,我想收到一条显示“找不到条形码”的消息 这是我要处理的文件的 link :

https://docs.google.com/spreadsheets/d/1r4fwLnkVI-Qt_f-gNtogkPgYMYEA44rg/edit?usp=sharing&ouid=118007873808820761459&rtpof=true&sd=true

这里是 excel 文件的截图:

代码是:

但是当我插入条形码时(对于存在于“C”列或其他不存在的条形码),我会给出输出:“找不到条形码”。 当 C 列中不存在插入的条形码并继续要求输入时,我喜欢得到“找不到条形码”

但是当我插入我确定存在的条形码时出现此错误:

有什么建议吗?

我认为这会起作用:

from numpy import int64 # handle large integers
import pandas as pd
from io import StringIO
import time


# df = pd.read_csv(StringIO(inp),sep=';')
df = pd.read_excel(r'C:\Users\dcg601\Downloads\test2.xlsx', header=None)
# df[2] = df[2].astype('int') # need to do that to enable comparisons
# df.iloc[:2]
df.iloc[:,6] = df.iloc[:,6].fillna(0)
df.iloc[:,2] = df.iloc[:,2].astype('str')
df.iloc[:,2] = df.iloc[:,2].str.strip('\u202a') # strips the right to left character
df.iloc[:,2] = df.iloc[:,2].astype('int64') # necessary cause numbers are too large

n = input("SCAN Barcode : ")
n = int64(n)
while n != 0 :
    if(n in df[2].values):
        df.loc[df[2]==int64(n),6]+=1
        df.to_excel(r'C:\Users\dcg601\Downloads\test2.xlsx',index=False,header=False)
    else :
        print("Barcode not found")
        time.sleep(5)
    print()
    n = input("SCAN Barcode : ")
    n = int64(n)

备注:

  1. df[2] = df[2].astype('int') 否则比较失败。 pandas 如果我没记错的话,默认值是字符串,
  2. 输入后你应该用n = int(n)转换成整数,否则输入的n是字符串,
  3. if 条件应该检查​​ n in df[column].values 而不仅仅是 n in df[column]

编辑:添加了一些 iloc,但它们可能不是很有必要,制作了一些条带和与 str

之间的转换