I'm trying to go thru a file with product codes and rateband prices and quantities
(productcode, pricea, qtya, priceb, qtyb, pricec, qtyc, priced, qtyd, pricee, qtye).
For a certain group of up to 4 productcodes I need to sum up the values of the price cells after testing the qty
fields have the same values.
Because I don't have any experience with python I was wondering what the most elegant way would be to do this in python.

Recommended Answers

All 4 Replies

I'm not entirely sure I follow but let's say you have (as in your example) 11 fields separated by commas. You're going to check if the value of the first element (index 0) has one of several values and if so, and if the value of field-x is equal to y, accumulate the value in field-z:

pcodes=(value1, value2, value3, value4)
qtys=(12,3,8,19)
prices=[0,0,0,0]
for strLine in fileobject:
    lstLine=strLine.strip('\n').split(',')
    if lstLine[0] in pcodes:
        for i in xrange(len(qtys)): 
            if lstLine[2+i*2]==qtys[i]: prices[i]+=lstLine[1+i*2]

Thanks, actually I have all these data in 1 table so it looks like this:
productcode, pricea, qtya, priceb, qtyb, pricec, qtyc, priced, qtyd, pricee, qtye
aa,10,5,9,10,8,50,7,100,6,500
ab,10,5,9,10,8,50,7,100,4,500
ba,10,5,9,10,8,50,8,100,6,500
bb,10,5,9,10,8,50,6,100,6,500
ca,10,5,9,10,8,50,7.5,100,6,500
cb,10,5,9,10,8,50,7.3,100,6,500
cc,10,5,9,10,8,50,7.2,100,6,500
What I like to achieve is for example sum prices for productcode aa, bb and cc.

You can go from there:

# data format:
# productcode, pricea, qtya, priceb, qtyb,
# pricec, qtyc, priced, qtyd, pricee, qtye
data = '''\
aa,10,5,9,10,8,50,7,100,6,500
ab,10,5,9,10,8,50,7,100,4,500
ba,10,5,9,10,8,50,8,100,6,500
bb,10,5,9,10,8,50,6,100,6,500
ca,10,5,9,10,8,50,7.5,100,6,500
cb,10,5,9,10,8,50,7.3,100,6,500
cc,10,5,9,10,8,50,7.2,100,6,500
'''

fname = "price_data.csv"
# write the test file
with open(fname, "w") as fout:
    fout.write(data)

# read the test file back in
with open(fname, "r") as fin:
    mylist = []
    for line in fin:
        # remove trailing new line char
        line = line.rstrip()
        line_list = line.split(',')
        # unpack line_list
        code, pa, qa, pb, qb, pc, qc, pd, qd, pe, qe = line_list
        # sum up the prices in each line
        #print(pa, pb, pc, pd, pe)  # test
        sum_prices = float(pa) + float(pb) + float(pc) + float(pd) + float(pe)
        #print(sum_prices)  # test
        # create a list of (productcode, sum_prices) tuples
        mylist.append((code, sum_prices))

# show result
import pprint
pprint.pprint(mylist)

'''my result >>>
[('aa', 40.0),
 ('ab', 38.0),
 ('ba', 41.0),
 ('bb', 39.0),
 ('ca', 40.5),
 ('cb', 40.3),
 ('cc', 40.2)]
'''

My question is, what do you do if the quantity fields don't match?

Thanks again, I'm impressed by the power of Python.
Actually I don't want to sum up the prices per line but this should be combined with my other question where I use an indexfield to get the productcodes from the arttabel.
For each artcode of and indexfield I sum the pricefields with the same name (pricea, priceb, pricec etcetera)
If the quantity fields don't match it should give an error message:
"Error ##### Rateband quantity mismatch"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.