0

I will try to keep this as brief as possible as I tried to find an answer and I couldn't. I am trying to create a simple till system. I am using a CSV file to store the description, price and stock of the product. If the user logs in as a manager they will be able to change the stock of a particular product. I find it very difficult to understand how to change the new stock. i forgot to mention that I use lists to store each category Let's say I have the following file

apples,1,11
grape,1.2,1
chocolate,0.75,15
bananas,1.35,27

And the following code to create the lists:

Products = []
Prices = []
Prices= []
import csv
with open('listlist.csv') as csvfile:
    readCSV = csv.reader(csvfile,delimiter=',')
    for row in readCSV:
        print(row)
        item = row[0]
        price = row[1]
        stock = row[2]
        Products.append(item)
        Prices.append(price)
        Stock.append(int(stock))

If the manager wants to change the stock of the item 'grape' from 1 to 11, how should I approach this in the easiest way?

2
  • Storing in three different lists in your program will make this task more difficult, a better data structure would be a dictionary where you store all the information together. There is csv.DictReader to facilitate handling your files this way. Commented Mar 18, 2018 at 17:02
  • You should probably consider using a popular relational database such as MySQL to handle this transaction instead of csv files. For example, how are you going to handle user logins and transactions? I think it will also be clearer how to change the price of stocks when you do this. Commented Mar 18, 2018 at 17:03

2 Answers 2

1

Since this appears to be a homework assignment or exercise, this isn't a full answer, but should get you started.

Reading your file, using list of dictionaries to store the items:

items_in_store = []
import csv
with open('listlist.csv') as csvfile:
    readCSV = csv.reader(csvfile,delimiter=',')
    for row in readCSV:
        item = dict(name=row[0], price=row[1], stock=row[2])
        items_in_store.append(item)

Looping over the resulting list, changing a specific target item:

tgt_item = 'apple'
tgt_stock = 5
for item in items_in_store:
    if item['name'] == tgt_item:
         # we found our item, change it
         item['stock'] = tgt_stock

To persist your changes to the file (note this time we open the file in write mode):

with open('listlist.csv', 'w') as csvfile:
    writeCSV = csv.writer(csvfile, delimiter=',')
    for item in items_in_store:
        row = [item['name'], item['price'], item['stock']]
        writeCSV.writerow(row)

Alternative approach, again reading the file, but this time storing in a dictionary containing dictionaries:

items_in_store = {} # create empty dictionary
import csv
with open('listlist.csv') as csvfile:
    readCSV = csv.reader(csvfile,delimiter=',')
    for row in readCSV:
        # use item name as key in first dictionary
        # and store a nested dictionary with price and stock
        items_in_store[row[0]] = dict(price=row[1], stock=row[2])

Having our data stored this way, we do not need to loop to change the stock, we can just access the desired item with its key right away:

tgt_item = 'apple'
tgt_stock = 5
items_in_store[tgt_item]['stock'] = tgt_stock

In all the above example snippets, you could ask for user input to fill your tgt_item and tgt_stock.

Sign up to request clarification or add additional context in comments.

3 Comments

This was just a simple example so I can explain the problem with my code. I was trying to omit pandas as I wanted to use lists. I did the exactly same thing as you with dictionaries but it doesn't work. When I reopen the CSV file the value that I want to change is still the same and I want the change to be permanent. It only changes when the program is ran.
@RebecaTeban This might be a stupid question, but did you actually save your data back to the file after changing it?
@RebecaTeban See the edit for a simple example for writing back changes with the first approach.
0

you could do it using pandas for example, and you wouldnt need to deal with different lists

       0         1       2
0   apples      1.00    11
1   grape       1.20    1
2   chocolate   0.75    15
3   bananas     1.35    27


import pandas
df =  pandas.read_csv(csv_file_path, header=None)
df.loc[df[0] == "apples", 2] = new_stock

the [0] and 2 can be changed by the name of the columns if you add column names to the file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.