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

哈米德·史密斯·萨利希

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

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

代码是:

代码的屏幕截图

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

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

错误截图

有什么建议么?

雅尼斯·P。

我认为这会起作用:

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')否则比较失败。如果我没记错的话,熊猫的默认值是字符串,
  2. 输入后,您应该使用 转换 ton 整数n = int(n),否则n输入是字符串,
  3. if条件应该检查​​,n in df[column].values不仅仅是n in df[column].

编辑:添加了一些ilocs 但它们可能没有太大必要,进行了一些剥离和转换str

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章