首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于搜索python代码的输出错误,并在excel中计算扫描的条形码。

用于搜索python代码的输出错误,并在excel中计算扫描的条形码。
EN

Stack Overflow用户
提问于 2022-04-30 11:54:43
回答 1查看 50关注 0票数 -1

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

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

但是,当我插入一个我确信存在的条形码时,出现了这个错误:

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-30 13:02:52

我认为这是可行的:

代码语言:javascript
复制
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. 输入后应该用n = int(n)转换成吨整数,否则n从输入是string,
  3. if条件应该检查n in df[column].values而不仅仅是n in df[column].values

编辑:添加了一些iloc,但它们可能不是什么必要的,做了一些条子和转换到和从str

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72068303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档