0

I have a csv file with string items in each file. (although some are integer inputs). I wish to subtract row[3] from row[5], but can't quite figure out the syntax to do so. Can anyone help?

print("PRINT RECORD" + '\t'+ row[0]+ '\t' + int(row[5])-int(row[3]))

The error message is:

ValueError: invalid literal for int() with base 10: 'Target Stock'

I've tried a number of different things which don't work

UPDATE

ID NO.     Description       Price  Current Stock   Re Order    Target Stock
12345670    x                  500            2         3          5
12121212    y                  900            1         3          5

I need the statement to subtract (in this example): 5-2 and then 5-1

UPDATE:

I tried

while row[5] and row[3] != str:
                    print("LOW STOCK" + '\t'+ row[0]+ '\t' + (int(row[5])-int(row[3])))

Still, the same error.

UPDATE I've added the header search...but the following error

 l=str(row[4])
        for row in reader:
            if(row[3])< (row[4]):
                has_header = csv.Sniffer().has_header(inf.read(1024))
                if has_header:
                    next(incsv)  # skip header row
                    lowstock="true"
                    targetstock_r= int((row[5]))
                    currentstock_r=int((row[3]))
                    reorderstock_r=int((row[4]))
                    writer.writerow(("LOWSTOCK", row[0],(targetstock)-(row[3])))

ERROR MESSAGE:

NameError: name 'inf' is not defined
  • That row item contains `'Target Stock'`, that can't be *casted* to an integer. You may want to review your csv file – Moses Koledoye Sep 26 '16 at 08:38
  • check the values in the respective coloumns as it clearly tells u that it can't cast an string into the integers – armak Sep 26 '16 at 08:39
  • 'Target Stock' is only the header...the rest of the elements under that heading contain only integers –  Sep 26 '16 at 08:41
  • 1
    @pythoncarrot Then skip the header – Moses Koledoye Sep 26 '16 at 08:42
  • something like a while loop? Again, syntax I'm not entirely sure of. while file_to_read != " ": ? –  Sep 26 '16 at 08:44
  • See [When processing CSV data, how do I ignore the first line of data?](http://stackoverflow.com/q/11349333/1314743) or [Skip the headers when editing a csv file using Python](http://stackoverflow.com/q/14257373/1314743). That was not really hard to find by searching for "python CSV skip header". – Martin Nyolt Sep 26 '16 at 08:57
  • Can you show some more code? How are you reading in the file? – monkut Sep 26 '16 at 09:08

0 Answers0