-2

I need to convert a .csv output into a string of keystrokes for a different program to perform automated billing. For the most part it would work if I only had to automate 4 at a time. That's the output from the regOutput() function. The software that actually functions as the POS has a quirk about advancing to the next page halfway through editing the 5th entry. So the pageChange() function needs to be what prints for every 5th row. so in the print function "P1" needs to be advanced to P2 for the tenth row, P3 for the 15th etc. and "5 CL" needs to advance to "10 CL" "15 CL" etc. also.

So I need to have that function run every five rows of the csv, and add. regOutput() can run for every row not divisible by 5.

So far i have this:

import csv
import sys

export = csv.DictReader(open("export.csv"),delimiter='\t')
sys.stdout = open('autobilling.txt','w')

def regOutput():
    for row in export:
        print row['SKU']
        print row['Qty']
        print ' '
        print ' '
        print 'A'
        print 'P'
        print row['productPriceSingle']
        print 'CL'
        print row['Order Store ID']

def pageChange():
    for row in export:
        print row['SKU']
        print row['Qty']
        print ' '
        print ' '
        print 'A'
        print 'P'
        print row['productPriceSingle']
        print ' '
        print 'P1'
        print 'M'
        print '5 CL'
        print row['Order Store ID']
        print ' '
        print 'A'

All help is appreciated. Thanks. Corey

Martin Evans
  • 45,791
  • 17
  • 81
  • 97

2 Answers2

0

There is more than one way to accomplish this, but one way is to use the enumerate function:

>>> for x in enumerate(["a", "b", "c"]):
...   print x
... 
(0, 'a')
(1, 'b')
(2, 'c')

Then the (index + 1) % 5 will 0 every 5th element.

Nathan Davis
  • 5,636
  • 27
  • 39
0

You could just run this using the one function as follows:

def regOutput():
    for index, row in enumerate(export):
        print row['SKU']
        print row['Qty']
        print ' '
        print ' '
        print 'A'
        print 'P'
        print row['productPriceSingle']

        if index % 5 == 4:
            print ' '
            print "P%u" % ((index+1) // 5) 
            print 'M'
            print '%u CL' % (index+1)
        else:
            print 'CL'

        print row['Order Store ID']

        if index % 5 == 4:
            print ' '
            print 'A'

This way the for loop will keep running smoothly. The enumerate function can be used to return a counter at the same time as the item being iterated which can be used to determine when to action your paging.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Worked perfectly, and I used your index+1 modified to change the page number too. thanks! – Corey Lloyd Aug 12 '15 at 13:50
  • Excellent, glad it solved your problem. Don't forget you can click the tick button to the left of an answer to accept. I've updated the solution with a possible page number fix too. – Martin Evans Aug 12 '15 at 17:30
  • This is how i fixed it. Which worked also, I no longer have to open Excel at all to bill out at the end of the day. First question on stack overflow and I'm a huge fan already. Added the commands for this to a .bat and and it's all operates with one click now. print 'P' + '%u' % ((index / 5)+1) – Corey Lloyd Aug 12 '15 at 19:46