Making a CSV files with data of Fibre rolls

One more thing: Phil would tell you to put that script as a function in the project script library, and call the function from the gateway event.

And I’d agree, seeing how I’ve had very confusing errors that were apparently caused by the VERY weird scoping behavior of gateway scripts.

4 Likes

Not to mention it’s the best way to keep your code DRY and organized.

3 Likes

I feel superfluous. :grin:

2 Likes

Hi all,

Thanks all for the help.
I still have some issues with the script “filePath” is not defined:

I think we are almost there. For the moment there is still no CSV file created at the directory.

Thanks for all the help!

The error message is quite clear. You’re trying to use a variable named filePath, but it doesn’t exist.
I’ll let you read the script and figure it out (hint: you have an unused variable with a very similar name)

1 Like

Hi Pascal,
The unused is fileName.
If I change (filePath,csv) to (fileName,csv), I still have problems.
It says I0Error, file not found :slight_smile:

Thanks!

Are you on Vision or Perspective ?

Vision :slight_smile:

Then I don’t think I can help you…
Maybe an issue with access rights ?

The line is running again.
I changed the code like this:


The issue now is:

I played myself a littlebit (you see which lines I put in comment) and the I got this out:

I am almost there I think, but still some strange things :slight_smile:

Hi,
I played a littlebit.
I have some values in Excel, but the problem is, every new roll the data of the previous roll got deleted and overwrite with the data from the last roll. So there is only one roll in the Excel.

https://docs.inductiveautomation.com/display/DOC81/system.file.writeFile

You need to pass append=True to the call to system.file.writeFile.

edit: If you’re appending data to a file, you might need a different function to generate that data, as you might want not to add the column names again. I think passing showHeaders=False to system.dataset.toCSV might be enough.

If I add the showHeaders, I still have the same problem.
I get the csv file, but still the same. Strange headers + every roll the previous roll is deleted.
So every new roll the previous roll is getting deleted.
Somebody have a clue? With the headers I can life :p.

Thanks for all the help

Did you try this ?

At this point, you should post a copy of your code. Not a screenshot. And format it like this:

```python
code
```

Those are backticks (just above the tab key on a qwerty layout), and there are 3 of them.

2 Likes
   #set a list of tag paths of all tag values needed in script and read them all at once
   tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
   #system.tag.readBlocking() returns a list of qualified values so this builds a list with just the values
   tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]

   headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
   #Get the current date, format it and add to the front of the tagValues list
   now = system.date.now()
   tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
   #crete the CSV file
   fileName = r"D:\TIS_TEST_" + system.date.format(now, "yyMMdd") + ".csv"
   csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False)
   system.file.writeFile(fileName,csv)
   #reset the "reset" tag.  system.tag.writeBlocking blocks the thread until the write has finished.
   system.tag.writeBlocking([str(event.getTagPath())],[0])

I hope It work (I have an Azerty :))

Thanks!

   tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
   tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]

   headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
   now = system.date.now()
   tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
   #crete the CSV file
   fileName = r"D:\TIS_TEST_" + system.date.format(now, "yyMMdd") + ".csv"
   csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False)
   system.file.writeFile(fileName,csv,True)
   system.tag.writeBlocking([str(event.getTagPath())],[0])

It might be ctrl-alt-7 on a French azerty.
You can specify the language after the first 3 backticks, to enable syntax highlighting.

system.file.writeFile(fileName, csv, append=True)

Hi,

So I tried to change:
system.file.writeFile(fileName, csv, append=True )
But like that it was not working, instead I did it like Irose:
system.file.writeFile(fileName,csv, True `)

I have all data of the rolls, but my lay out is not ok:
image
Any idea how to make it nice? I mean only one header colum, remove the colums with str, str, D, D and also remove the columns with the #

My script is this:

if not initialChange and newValue.getValue():

tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]

tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]

headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]

now = system.date.now()

tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues

fileName = r"D:\TIS_TEST_" + system.date.format(now, "yyMMdd") + ".csv"

csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False)

system.file.writeFile(fileName,csv,True)

system.tag.writeBlocking([str(event.getTagPath())],[0])

try csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues, tagValues), True, False, False) instead.

And as @lrose just said, and I warned you about a few posts above, you need your subsequent calls to not include the headers, otherwise you're appending headers every time.

I was going to recomend using system.dataset.toExcel(), however, I couldn’t get that to append correctly to a file.

Since what you’re working with here isn’t very much data we can pretty easily just build the csv string. We will also only add the headers if the file doesn’t exist already.

tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
tagValues = [system.date.format(system.date.now(),"dd/MM/yyyy"),system.date.format(system.date.now(),"HH:mm:ss")] + tagValues

#create the csv file
fileName = r"D:\TIS_TEST_" + system.date.format(system.date.now(), "yyMMdd") + ".csv"
csv = ''
#this checks to see if the file exists and if it doesn't adds the headers
if not system.file.fileExists(fileName):
    csv = ','.join(["Datum","Tijd","LENGTE","GEWICHT"])
#this adds the actual data to the string.
csv = '\n' + ','.join(tagValues)
system.file.writeFile(fileName,csv,True)
system.tag.writeBlocking([str(event.getTagPath())],[0])