Mass import of UDT tags via simple csv and script (example)

#1

There are SO many posts in here regarding tag imports, csv formats, xml schema, and the issues regarding them, so I figured I would post a very simple solution that was born out of my own issues with tag imports.
The problem with the legacy csv import is that it is quite un-intuitive when it comes to the format when you are trying to write parameter values and tag values (overrides) along with the tag. The format becomes multi-line and somewhat cryptic.
The issue with using xml is that unless you’re used to working with xml, or have a schema to work from, the xml file (even in excel as an editor) is multi-line and again, somewhat cryptic.

I finally settled on the fact that the only solution (for me) was to create a script (thanks to a quick post reply by @PGriffith ) that would use a SIMPLE, single-line-per-tag csv file to do the heavy lifting. Here is an example of what I’ve put together for anyone that wants to do the same (you can also read my blog post about it).

import csv	#import the python csv library
# Script tools to open csv file and import tags of specified UDT Type

# Open csv file through file selector
def openFile():
	filePath = system.file.openFile("csv","%user%")
	csvFile = open(filePath, 'r')
	reader = csv.DictReader(csvFile)
	print reader.fieldnames
	for row in reader:
		# Call addUDT function based on UDTType
		if row['UDTType'] == 'AIn': addAIn( row )
		# add more conditions for more UDTTypes...

# Function to add instance tag of UDT type 'AIn'
def addAIn( csvRow ):
	if csvRow['TagProvider'] == "": tagProv = '[default]'
	else: tagProv = '[' + csvRow['TagProvider'] + ']'
	tagPath = tagProv + csvRow['TagPath']
	tagName = tagPath + '/' + csvRow['Name']
	
	if system.tag.exists(tagName):
		system.tag.removeTag(tagName)
		print tagName + ' removed'

	system.tag.addTag( 	parentPath = tagPath,
						name = csvRow['Name'],
						tagType = 'UDT_INST',
						attributes={'UDTParentType':csvRow['UDTType']},
						parameters={'Description':csvRow['Description'],
									'EquipGp':csvRow['EquipGp'],
									'ItemName':csvRow['ItemName'],
									'RawLocation':csvRow['RawLocation'],
									'EngUnits':csvRow['EngUnits']
									},
						overrides={
										'PriorityHH':{'value':csvRow['PriorityHH']},
										'PriorityH':{'value':csvRow['PriorityH']},
										'PriorityL':{'value':csvRow['PriorityL']},
										'PriorityLL':{'value':csvRow['PriorityLL']}
									}
						)
	print 'AIn tag ' + tagName + ' added'

The advantage with this is that the csv is very simple. I imported a csv with 351 instances of 9 different UDTs, contained in 16 headers and in the process, updated 2457 values (parameters & member tag values).
It’s just a way to keep things simple, but doing it efficiently and accurately.

[Edit]: I put this in a Project Library script and run it from the Script Console.

2 Likes

Bug? - UDT Instance import via csv not importing parameter values anymore
#2

Thanks for sharing!

I think with some minor tweaks this can be really helpful for me!

Are you using the standard csv format output when you export tags to csv?

0 Likes

#3

Nope. That’s the sweet part. You simply create a csv of your own format with only the information you need to create your tags.
For example:
Based on what your script handles internally, you may have the following headers in your csv:

    [provider]    ,        [path]         ,     [tagname]      ,   [UDTtype]    ,   [param1]    ,   [param2]

so your csv would look like:

    [provider]     ,           [path]        ,   [tagname]   ,   [UDTtype]    ,     [param1]    ,   [param2]
    '[default]'    ,   root/folder/folder    ,   tagname1    ,     myUDT      ,       blue      ,       red
    '[default]'    ,   root/folder/folder    ,   tagname2    ,     myUDT      ,      green      ,       red
    '[default]'    ,   root/folder/folder    ,   tagname3    ,     myUDT2     ,       blue      ,       red
    '[default]'    ,   root/folder/folder    ,   tagname4    ,     myUDT      ,       blue      ,       red

You’re in charge of what you need/want in there, whatever makes your life easier.

2 Likes