-1

Looking to have my code read one text file and store the line number of a user input as num and then use the variable num to read the same line on another file. currently, the code for the first step of reading the first text file is working and has been tested but the second part doesn't display anything after being executed. I have changed multiple things but am still stuck. Help would be much appreciated.

here is my code:

print("Check Stock")
ca = input("Check all barcodes?")
if ca == "y":
    for x in range(0,5):
        with open ("stockbarcodes.txt") as f:
            linesa = f.readlines()
            print(linesa[x])

        with open ("stockname.txt") as f:
            linesb = f.readlines()
            print(linesb[x])
            print(" ")

else:    
    bc = input("Scan barcode: ")
    f1 = open ("stockname.txt")
    for num, line in enumerate(f1, 1):
        if bc in line:
            linesba = f1.readlines()
            print(linesba[num])
glibdud
  • 7,550
  • 4
  • 27
  • 37
  • 1
    Perhaps, you could consider rewording your question. As of now, it looks unclear to me what are you trying to do. – norok2 Jul 11 '18 at 14:56
  • I don't see where you're storing the *line number*. I can see you're accepting the line content as input in your else statement, but you just use `num` to iterate - you never store anything in it? – R Balasubramanian Jul 11 '18 at 14:56
  • I'm not sure, if I understand correctly what you are trying to do...If for example the user input is `8`, do you want to read the 8th line of "stockname.txt" or do you want to find the line in which 8 is in "stockbarcodes.txt" and then read this line from "stockname.txt"? I think the latter would make more sense right, but your code does not look like that is your goal... – lkriener Jul 11 '18 at 15:10

1 Answers1

0

As user Ikriemer points, it seems that you want to retrieve the stock name based on the barcode. For that kind of task you rather create a normalized Data Base, which discribes Entities, Properties and relationships. As you can se here there are a lot of things to take into account.

This code was tested on Mac OS, but considering OP's comment (who seems to be using windows), it is ok if the dtype is not specified.

Considering that the above solution may not be as quick as you like, you also have two options.

First option
As I can not check the content of your example files, the strategy that you show in your code makes me believe that your assuming both files are ordered, in a way that first line of the barcode file corresponds to first item in the stock name file. Given that, you can query the index of an element (barcode) in an array like data structure, and retrieve the element of another array (name) stored in the same position. Code below:

import numpy as np


print("Check Stock")
ca = input("Check all barcodes? (y/n): ")
if ca == "y":
    for x in range(0, 5):
        with open("stockbarcodes.txt") as f:
            linesa = f.readlines()
            print(linesa[x], sep="")

        with open("stockname.txt") as f:
            linesb = f.readlines()
            print(linesb[x], sep="")
            print(" ")
else:
    try:
        codes = np.genfromtxt("stockbarcodes.txt").tolist()
        names = np.genfromtxt("stockname.txt", dtype=np.str).tolist()
        bc = input("Scan barcode: ")
        index = codes.index(int(bc))
        print(names[index])
    except IndexError:
        print("Bar code {} not found".format(bc))

Second option
This option could be considered a workaround method to a data base like file. You need to store your data in some way that you can search the values associated with an specific entry. Such kind of tasks could be done with a dictionary. Just replace the else clause with this:

else:
    try:
        codes = np.genfromtxt("stockbarcodes.txt").tolist()
        names = np.genfromtxt("stockname.txt", dtype=np.str).tolist()
        table = {k: v for k, v in zip(codes, names)}
        bc = input("Scan barcode: ")
        print(table[int(bc)])
    except KeyError:
        print("Bar code {} not found".format(bc))

Again, in the dictionary comprehension we are assuming both files are ordered. I strongly suggest you to validate this assumption, to warranty that the first bar code corresponds to the first stock, second to second, and so on. Only after that, you may like to store the dictionary as a file, so you can load it and query it as you please. Check this answer fot that purpose.

Ivan Calderon
  • 580
  • 6
  • 14
  • from using your code I get the error of: Traceback (most recent call last): File "C:\Users\harve\Documents\Stock Inventory\invmantest.py", line 40, in codes = np.genfromtxt("stockbarcodes.txt", dtype=np.int32).tolist() File "C:\Users\harve\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\npyio.py", line 2149, in genfromtxt output = np.array(data, dtype) OverflowError: Python int too large to convert to C long – Harvey Gillett Jul 12 '18 at 09:17
  • That error is related to the dtype parameter inside np.genfromtxt. It is ok if you let numpy guess the dtype for you. Please see the edit. – Ivan Calderon Jul 12 '18 at 14:57