jeudi 18 novembre 2021

How to replace values in csv files converting one of them in a dictionary?

I am trying to replace the req and price with PRODUCT_INDEX and PRICE_INDEX, but the way I am doing it just brings the output of the first line in the list, I want to replace it with the one who is on the dictionary (that is what I tried to do with the if statement).

import csv

PRODUCT_INDEX = 0
NAME_INDEX = 1
PRICE_INDEX = 2

REQ_PROD_INDEX = 0
QUANT_INDEX = 1

def main():
    products = read_products('products.csv')
    
    print('Products')
    print(products)


    with open("request.csv", "rt") as request_file:
        reader = csv.reader(request_file)
        next(reader)
        
        values = list(products.values())
        names_values = [row[0] for row in values]
        price_values = [row[1] for row in values]
        value1 = names_values[0-4]
        value2 = price_values[1]
        
        for row in reader:
            req = row[REQ_PROD_INDEX]
            if req in products:
                req = names_values[0]
                quantity = row[QUANT_INDEX]
                price = value2

                print(f'{req}: {quantity} at {price}')
            
   
    main()

Aucun commentaire:

Enregistrer un commentaire