我有一个 xlsx 文件,它的“C”列包含事物的条形码,我想插入条形码来计算它们(每个条形码的数量将放在“G”列中的相应单元格中)主要问题是:我想接收当插入的条形码在“C”列的任何单元格中不存在时显示“未找到条形码”的消息这是我要处理的文件的链接:
代码是:
但我会在插入条形码时给出输出:“找不到条形码”(对于那些存在于“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)
笔记:
df[2] = df[2].astype('int')
否则比较失败。如果我没记错的话,熊猫的默认值是字符串,n = int(n)
,否则n
输入是字符串,if
条件应该检查,而n in df[column].values
不仅仅是n in df[column]
.编辑:添加了一些iloc
s 但它们可能没有太大必要,进行了一些剥离和转换str
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句