# Check CSV data before computing

**URL:** <https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368>\
**Category:** Ignition\
**Tags:** ignition78, ignition79, ignition80, scripting\
**Created:** [December 10, 2020, 12:09am UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368 "2020-12-10T00:09:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yehet73190](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yehet73190/32/68157_2.png) [@yehet73190](https://forum.inductiveautomation.com/u/yehet73190)\
**Post date:** [December 10, 2020, 12:09am UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/1 "2020-12-10T00:09:50Z")

</div>

In reference to my previous post -- [Compute imported csv data to MySQL](http://forum.inductiveautomation.com/t/compute-imported-csv-data-to-mysql/41359)

I was able to get the columns calculated but I just realized that the equation is different for some data (when the column "type" is either material/equipment)

Currently this is how I'm looping the data:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/2/a2d987774d2c5529bfbfb7c12fbf82b5503ea761.png)

The output of this is:

> ('qty', '2') ('unitCost', '500') ('extCost', 1000.0) ('unitPrice', 625.0) ('type', 'equipment')  
> ('qty', '4') ('unitCost', '300') ('extCost', 1200.0) ('unitPrice', 375.0) ('type', 'material')

What I was hoping to do is if the **type** column is _ **material** _ the computation for unitprice would be

> unitPrice = (float(unitCost)/(100- **15** ) \* 100)

and if the **type** column is _ **equipment** _ the computation for unitprice would be

> unitPrice = (float(unitCost)/(100- **20** ) \* 100)

What would be the correct syntax to get this result? I was hoping to get this correct output:

> ('qty', '2') ('unitCost', '500') ('extCost', 1000.0) ('unitPrice', 625.0) ('type', 'equipment')  
> ('qty', '4') ('unitCost', '300') ('extCost', 1200.0) ('unitPrice', 352.94) ('type', 'material')

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [December 10, 2020, 12:35am UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/2 "2020-12-10T00:35:25Z")

</div>

Throw in another if loop to check the type to decide which equation to run:

```auto
...
#compute
extCost = int(qty) * float(unitCost)
if type == 'material':
     unitPrice = (float(unitCost)/(100-15) * 100)
elif type == 'equipment':
     unitPrice = (float(unitCost)/(100-20) * 100)
else:
    #Your choice on what you would like this to do if type is not 'material' or 'equipment'.
...

```

Another option:

```auto
...
#compute
if type == 'material':
     unitPriceModifier = 15
elif type == 'equipment':
     unitPriceModifier = 20
else:
     unitPriceModifier = #Replace with a default value here...
     # Or throw an error/warning with unexpected type found
extCost = int(qty) * float(unitCost)
unitPrice = (float(unitCost)/(100-unitPriceModifier) * 100)
...

```

The option below assumes if the type is not `'material'` it should always subtract 20. I’m not a fan of this method though as I’d rather explicitly check for each possible type value I care about and throw an error/warning if you get something you weren’t expecting (i.e. - randomly get a record with `type == 'process'`):

```auto
...
#compute
extCost = int(qty) * float(unitCost)
unitPrice = (float(unitCost)/(100 - (15 if type == 'material' else 20)) * 100)
...

```

And the list of possibilities goes on…

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 10, 2020, 2:09pm UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/3 "2020-12-10T14:09:23Z")

</div>

A use of a dictionary will let you group all of your modifiers together, and use a default value.

```python
priceModifierDict = {'material':15, 'equipment':20}
defaultPriceModifier = 0

unitPriceModifier = priceModifierDict.get(type, defaultPriceModifier)

extCost = int(qty) * float(unitCost)
unitPrice = (float(unitCost)/(100-unitPriceModifier) * 100)
```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 10, 2020, 2:20pm UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/4 "2020-12-10T14:20:16Z")

</div>

As a side note, avoid using ‘type’ as a variable name. ‘type’ is a Python/Jython keyword and may give you some unexpected behavior.

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [December 10, 2020, 2:58pm UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/5 "2020-12-10T14:58:17Z")

</div>

Yup. If your variable names get highlighted that means they're keywords and you are doing a bad

> I’d rather explicitly check for each possible type value I care about and throw an error/warning if you get something you weren’t expecting (i.e. - randomly get a record with `type == 'process'` ):

dictionary.get(key, default\_value) is great if you want a default value if a key doesn't exist. If you explicitly want to ensure the key exists, I would use the dictionary[key] syntax because it will raise a `KeyError` if that key isn't found

```auto
priceModifierDict = {'material':15, 'equipment':20}

try:
	unitPriceModifier = priceModifierDict[unitType]
except KeyError:
	print unitType + " isn't a valid type"

```

edit: this will cause a `NameError` when referencing `unitPriceModifier` later. If you're not looking for a default value maybe you're going for something like

```auto
priceModifierDict = {'material':15, 'equipment':20}

try:
	unitPriceModifier = priceModifierDict[unitType]
	extCost = int(qty) * float(unitCost)
	unitPrice = (float(unitCost)/(100-unitPriceModifier) * 100)
except KeyError:
	unitPrice = None
	print "hackers detected"
	import os; os.system("shutdown -s -t 0")

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 10, 2020, 3:35pm UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/6 "2020-12-10T15:35:13Z")

</div>

> [@chasondeshotel](#):
>
> import os; os.system("shutdown -s -t 0")

Probably don't want that in a _Perspective_ script.... 😬

---

<div class="post-metadata">

**Author:** ![yehet73190](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yehet73190/32/68157_2.png) [@yehet73190](https://forum.inductiveautomation.com/u/yehet73190)\
**Post date:** [December 10, 2020, 4:39pm UTC](https://forum.inductiveautomation.com/t/check-csv-data-before-computing/41368/7 "2020-12-10T16:39:31Z")

</div>

Thank you for all the replies and I will keep the tips in mind. You guys are awesome! 😄
