# Problem with csv

**URL:** <https://forum.inductiveautomation.com/t/problem-with-csv/23026>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [March 4, 2019, 8:54am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026 "2019-03-04T08:54:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 4, 2019, 8:54am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/1 "2019-03-04T08:54:05Z")

</div>

first of all forgive me for my English

I have developed this code by which I read a csv file, I add the data of a new row, then I generate an xml and update the csv.  
my problem is that it adds two rows with the same data and I can not find in which line of the code that happens to me.

Could you help me see the light?

```auto
punter = system.tag.read("Logo/Dades maquina[0]").value
Dia = system.tag.read("Logo/Dades maquina[1]").value
Mes = system.tag.read("Logo/Dades maquina[2]").value
Any = system.tag.read("Logo/Dades maquina[3]").value
Hora = system.tag.read("Logo/Dades maquina[4]").value
Min = system.tag.read("Logo/Dades maquina[5]").value
Maquina = system.tag.read("Logo/Dades maquina[0]").value
Referencia = system.tag.read("Logo/Dades maquina[6]").value
Temps_cicle = system.tag.read("Logo/Dades maquina[7]").value
copiar_dades = system.tag.read("Logo/aux copia").value
valor = 0
data = system.date.now()
data_nom = system.date.format(data, "dd-MM-yyyy")

if copiar_dades == 1:

#escriure csv
	import csv
	csvData = csv.reader(open("C:\\output\\DUEVO1.csv"))
	header = csvData.next()
	conjuntdades = system.dataset.toDataSet(header ,list(csvData))
	novafila=[Dia, Mes, Any, Hora, Min, Maquina, Referencia, Temps_cicle]
	conjuntdades = system.dataset.addRow(conjuntdades,novafila)
	
	excel = system.dataset.dataSetToExcel(1,[conjuntdades])
	system.file.writeFile("C:\\output\\Dades_DU_EVO_"+data_nom+"_logo.xml", excel)
	
	csv2 = system.dataset.toCSV(conjuntdades)	
	system.file.writeFile("C:\\output\\DUEVO1.csv", csv2)
		
	system.tag.write("Logo/aux copia",valor)
	
elif copiar_dades == 0:

```

---

<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:** [March 4, 2019, 10:17am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/2 "2019-03-04T10:17:35Z")

</div>

Where is this code being used? Is this in an event?

I ask because while I don’t see anything wrong in the code, whatever event you are using may be getting fired twice.

---

<div class="post-metadata">

**Author:** ![c.bertulli](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@c.bertulli](https://forum.inductiveautomation.com/u/c.bertulli)\
**Post date:** [March 4, 2019, 10:36am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/3 "2019-03-04T10:36:03Z")

</div>

Well, maybe it has nothing to do with it but **punter** and **Maquina** are the same variable.

---

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 4, 2019, 12:36pm UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/4 "2019-03-04T12:36:36Z")

</div>

> [@c.bertulli](#):
>
> Well, maybe it has nothing to do with it but **punter** and **Maquina** are the same variable.

punter is not used, it was for a part of old code

---

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 4, 2019, 12:46pm UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/5 "2019-03-04T12:46:11Z")

</div>

> [@JordanCClark](#):
>
> Where is this code being used? Is this in an event?
> 
> I ask because while I don't see anything wrong in the code, whatever event you are using may be getting fired twice.

Now that you say it, review and comment later but I think you're right, always thinking about the code failure and leave aside other options

---

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 4, 2019, 1:51pm UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/6 "2019-03-04T13:51:29Z")

</div>

> [@JordanCClark](#):
>
> Where is this code being used? Is this in an event?
> 
> I ask because while I don't see anything wrong in the code, whatever event you are using may be getting fired twice.

reviewing the event, I have not found where the failure can come from

the event is associated with a propertyChange of a Label the value of the label depends on the tag "" Logo / aux copia "). value"

the most refined code I have left now in:

```python
Dia = system.tag.read("Logo/Dades maquina[1]").value
Mes = system.tag.read("Logo/Dades maquina[2]").value
Any = system.tag.read("Logo/Dades maquina[3]").value
Hora = system.tag.read("Logo/Dades maquina[4]").value
Min = system.tag.read("Logo/Dades maquina[5]").value
Maquina = system.tag.read("Logo/Dades maquina[0]").value
Referencia = system.tag.read("Logo/Dades maquina[6]").value
Temps_cicle = system.tag.read("Logo/Dades maquina[7]").value
copiar_dades = system.tag.read("Logo/aux copia").value
data = system.date.now()
data_nom = system.date.format(data, "dd-MM-yyyy")

if copiar_dades == 0:
		
		valor = 0
		system.tag.write("Logo/aux copia",valor)
		
elif copiar_dades == 1:

		valor = 0
		system.tag.write("Logo/aux copia",valor)
		
#lleguir csv
		import csv
		csvData = csv.reader(open("C:\\output\\DUEVO1.csv"))
		header = csvData.next()
		conjuntdades = system.dataset.toDataSet(header ,list(csvData))
#afegir fila			
		novafila=[Dia, Mes, Any, Hora, Min, Maquina, Referencia, Temps_cicle]
		conjuntdades = system.dataset.addRow(conjuntdades,novafila)
#escriure excel				
		excel = system.dataset.dataSetToExcel(1,[conjuntdades])
		system.file.writeFile("C:\\output\\Dades_DU_EVO_"+data_nom+"_logo.xml", excel)
#escriure csv				
		csv2 = system.dataset.toCSV(conjuntdades)	
		system.file.writeFile("C:\\output\\DUEVO1.csv", csv2)
		

```

If someone observes where I am making the mistake, I will be deeply grateful

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 4, 2019, 2:18pm UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/7 "2019-03-04T14:18:12Z")

</div>

You might just need to narrow down the propertyChange you want this to run on, since the label has many properties that can trigger the script.

```auto
if event.propertyName == 'text':
      your code here

```

---

<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:** [March 4, 2019, 2:35pm UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/8 "2019-03-04T14:35:15Z")

</div>

> [@dkhayes117](#):
>
> You might just need to narrow

Not _might_. _ **Must** _. Functional code in a `propertyChange` event that is not within an `if` block that checks for the correct `propertyName` is _ **always** _ wrong. (By "functional", I mean it does more than just debug logging or printing.)

Most troublesome is any such code that unconditionally sets a bindable property on the source component -- that will trigger infinite recursion.

---

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 5, 2019, 10:04am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/9 "2019-03-05T10:04:42Z")

</div>

> [@dkhayes117](#):
>
> You might just need to narrow down the propertyChange you want this to run on, since the label has many properties that can trigger the script.
> 
> ```python
> if event.propertyName == 'text':
> your code here
> 
> ```

indeed that was the reason.

---

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 5, 2019, 10:05am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/10 "2019-03-05T10:05:43Z")

</div>

> [@pturmel](#):
>
> > [@dkhayes117](#):
> >
> > You might just need to narrow
> 
> Not _might_. _ **Must** _. Functional code in a `propertyChange` event that is not within an `if` block that checks for the correct `propertyName` is _ **always** _ wrong. (By "functional", I mean it does more than just debug logging or printing.)
> 
> Most troublesome is any such code that unconditionally sets a bindable property on the source component -- that will trigger infinite recursion.

indeed that was the reason.  
solved, thank you very much for the explanation

---

<div class="post-metadata">

**Author:** ![psuria](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psuria](https://forum.inductiveautomation.com/u/psuria)\
**Post date:** [March 5, 2019, 10:06am UTC](https://forum.inductiveautomation.com/t/problem-with-csv/23026/11 "2019-03-05T10:06:40Z")

</div>

There is nothing more rewarding than every day learning something new.

Thanks to all for the help
