Trying to go thru the contents of a table with the following layout
indexfield1,art1,art2,art3,art4
In this table indexfield and art1 are always filled art2, art3, and art4 can be empty.
In pseudo-code this is what I want to do:

for each art of table where indexfield = x:
   do bladibla
   if last of x:
      do blodiblo

How can I do this in Python ?

Since you title your post with "...csv file", I'll assume you're reading this table one row at a time from a comma delimited file. So, let's say you've opened the file as fid (i.e., fid=open(<file name>)). Then you read through the file, strip off the line feed, split the line on commas and process the resultant list:

for strLine in fid:
    lstLine=strLine.strip('\n').split(',')
    indexfield=lstLine[0]
    for elem in lstLine[1:]:
       #do your thing
    #do your other thing

How can I fill the value of the indexfield to use for the search ?
Something like:

if lstLine[0]="ab":
for strLine in fid:
    lstLine=strLine.strip('\n').split(',')
    if lstLine[0]=="ab":
       for elem in lstLine[1:]:
          #do your thing
       #do your other thing
    #do something entirely else.

Wow, thank you so much.
Very cool language !

Your code works very well for part 1.
Now I want to take it to the next level.
This is the content of fid:
USOR2GB,USMEG1UD,USHOODBA
USOR4GB,USMEG2UD,USHOODBA
USOR8GB,USMEG3UD,USHOODBA
USOR16GB,USMEG4UD,USHOODBA
USOR32GB,USMEG5UD,USHOODBA
USOR64GB,USMEG6UD,USHOODBA
This is the content of fname:
test,pricea,qtya,priceb,qtyb,pricec,qtyc
perrytest,11,5,5,10,3,50
testrow2,12,5,10,10,9,50
USMEG1UD,10,5,9,10,8.75,50
USMEG2UD,10,5,9,10,8.75,50
USMEG3UD,10,5,9,10,8.75,50
USHOODBA,0.25,5,0.23,10,0.22,50
PRDOUSOB,10,5,9,10,8.75,50
testrow4,10,5,9,20,8.75,50
testrow5,10,5,9,10,8.55,50

This is your code for part1:

     for strLine in open(fid):
        lstLine=strLine.strip('\n').split(',')
        indexfield=lstLine[0]
        if indexfield=="USOR2GB":
           for elem in lstLine[1:]:
              print elem

Instead of print elem I want to use elem as index-field for the fname table
to pick up the price and quantity fields per product. So in the example where
indexfield="USOR2GB" it should fetch USMEG1UD and USHOODBA records from fname (and after testing the qty-columns it should sum pricea of USMEG1UD and USHOODBA, sum priceb of USMEG1UD and USHOODBA etcetera and store list of "summed prices" per indexfield ("USOR2GB" in this example) with indexfield as variable name).
Hopefully it's not getting too confusing.

Thanks pyTony, interesting indeed.
Do you have any experience with multiple csv files where
one is a dictionary with multiple values associated with each key
and these values are used as key in a secondary csv file ?

This is what I figured out so far.
My Python program works, now the challenge is to combine it with Scribus template and generate a pdf.

for strLine in open(arttable):
    lstLine=strLine.strip('\n').split(',')
    indexfield=lstLine[0]
    paf = pbf = pcf = pdf = pef = pff = pgf = phf = 0
    for elem in lstLine[1:]:
          for item in open(fname):      
            lstData=item.strip('\n').split(',')
            andexfield=lstData[0]
            if andexfield == elem:
              pa = lstData[1]
              qa = lstData[2]
              pb = lstData[3]
              qb = lstData[4]
              pc = lstData[5]
              qc = lstData[6]
              pd = lstData[7]
              qd = lstData[8]
              pe = lstData[9]
              qe = lstData[10]
              pf = lstData[11]
              qf = lstData[12]
              pg = lstData[13]
              qg = lstData[14]
              ph = lstData[15]
              qh = lstData[16]
              print andexfield                     
              print pa, pb, pc, pd, pe, pf, pg, ph
              print qa, qb, qc, qd, qe, qf, qg, qh

Assuming you don't need the data after printing it out, I would recommend shortening the code thusly:

            if andexfield == elem:
                print andexfield
                for p in range(1, 17, 2):
                    print p,
                print
                for q in range(2, 17, 2):
                    print q,
                print

The third argument of the range() function is an optional 'step' value, indicating how many values it should increment by each time; for example, range(0, 10, 2) would (in Python 2.x) give a list of [2, 4, 6, 8] as its result (in Python 3.x, range() is a generator; the values returned are the same, but it returns only one value per call, rather than a list).

Thanks, nice programming. Unfortunately I need the data, printing them was just for debugging purposes.

It would still be possible to use the same basic loops to, say, gather the data into a pair of lists. Still, it all depends on just what you need to do with it, I suppose.

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.