我有一个xlsx文件,它的"C“列包含事物的条形码,我想插入条形码来计数它们(每个条形码的编号将放在"G”列中相应的单元格中),主要问题是:当插入的条形码在"C“列的任何单元格中不存在时,我想接收一条显示”条形码未找到“的消息。
但是,当我插入条形码时,我会给出输出:“条形码未找到”(对于"C“列中的条形码或其他存在的条形码,t)。当插入的条形码在C列中不存在时,我喜欢得到“未找到条形码”,并继续请求输入
但是,当我插入一个我确信存在的条形码时,出现了这个错误:
有什么建议吗?
发布于 2022-04-30 13:02:52
我认为这是可行的:
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从输入是string,if条件应该检查n in df[column].values而不仅仅是n in df[column].values编辑:添加了一些iloc,但它们可能不是什么必要的,做了一些条子和转换到和从str
https://stackoverflow.com/questions/72068303
复制相似问题