Problem with csv

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?

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:

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.

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

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

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:

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

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.

if event.propertyName == 'text':
      your code here
1 Like

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.

4 Likes

indeed that was the reason.

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

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

Thanks to all for the help

1 Like