Rebeca Teban
Rebeca Teban

Reputation: 1

Changing existing CSV file depending on user input

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?

Upvotes: 0

Views: 1175

Answers (2)

bgse
bgse

Reputation: 8587

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.

Upvotes: 1

saucoide
saucoide

Reputation: 11

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

Upvotes: 0

Related Questions