Browse all tags of certain name and export to Dataset

Hi,

I’m trying to browse through all of my tags and read all of the tags named “override” i have in valve tag folders that currently have the state “False” (Every valve has a folder, with common tags such as “override”, “open”, “close” etc).

Currently I am able to navigate down through folders and subfolders using system.tag.browseTags

tags = system.tag.browseTags(parentPath="[SQLTags]AcuteManualFilling/MainProgram", tagPath="*XCV*")
for tag in tags:
	c = tag.fullPath
	
    	tags1 = system.tag.browseTags(parentPath=(c), tagPath="*Override*")
    	for tag in tags1:
		#	print tag.name
			a = tag.fullPath

This returns the following (Which is what I want to see)


[u'AcuteManualFilling/MainProgram/XCV857/Override']
[u'AcuteManualFilling/MainProgram/XCV856/Override']
[u'AcuteManualFilling/MainProgram/XCV855/Override']
[u'AcuteManualFilling/MainProgram/XCV854/Override']
[u'AcuteManualFilling/MainProgram/XCV853/Override']
[u'AcuteManualFilling/MainProgram/XCV852/Override']
[u'AcuteManualFilling/MainProgram/XCV859/Override']
[u'AcuteManualFilling/MainProgram/XCV858/Override']

I then use system.tag.read(a).value, which tells me if the tag is True or False.

I use an IF statement and in this I want to append the tag path to a dataSet, to list all of the override tags that are “False”

c = tag.path
			if values == False:
 				#print c
 				header = ["Valve"]
				data = []
				data.append(c)
				#print data	

			overrides = system.dataset.toDataSet(header, [data])

However, only one line appends to the dataset (the first tag detected):
u'AcuteManualFilling/MainProgram/XCV857/Override'

Is there a way to populate multiple rows with every override tag path that has been detected as False?

It looks like the data is being reinitialized to empty inside of your loop. The data and header initialization should be placed outside of the loop. Try something like this instead (not tested).

header = ['Valve']
data = []
for tag in tags:
    value = system.tag.read(tag.path).value
    if value == False:
		c = tag.path
		#print c
		data.append(c)
#print data

Hey Nick,

Thanks for the response. I am getting the following output:

[SQLTags]AcuteManualFilling/MainProgram/XCV857/Override
AcuteManualFilling/MainProgram/XCV857/Override
Traceback (most recent call last):
File “”, line 20, in
IndexError: Row 0 doesn’t have the same number of columns as header list.

So it still seems to be only catching the first “Override” instance it sees.

As for the indexError. Does the number of rows in a dataset need to be specified? Does it not just auto append each “Override” valve as a new row (Atleast that’s what I want to happen!)

You’re both halfway right. You do need to initialize data outside the loop - however, you also want to append each row as it’s own row object, due to the way system.dataset.toDataSet works; you need to construct a “two dimensional” array, which in Python is represented as a list of lists:

[
  [1], # row1 - should be as many elements as there will be columns in the output
  [2], # row2 - must match row1's length
  [3]  # row3
]

Leave data as @nick.schuetz had it - initialize it as an empty list before starting the loop. Then change the line after header = ["Valve"] to data.append([c]) - data will be your list of rows, and then you will append each row to it, as a unique list.
Then drop the square brackets in the call to system.dataset.toDataSet, since you already have a 2d array/list.

Thanks @PGriffith!

It worked a charm and thanks for the informative explanation

1 Like