How to get Python data set into a Dataset Memory Tag?

Post your code as formatted text, see Wiki - how to post code on this forum. It lets people quickly browse your code and configurations without having to download a file. This also helps with getting more people to answer questions.

Moving on to your actual attempt:

When importing, don't convert the csv into a dataset, just pull the raw values and pass them to system.tag.writeBlocking as the list of values for the list of tags. Trim down the list of values to match the number of tags you have or you'll get an error.

And for personal recommendations:

First off, don't trim the CSV that system.dataset.toCSV gives you, if you keep it in the default 'Dataset as a CSV' style it gives you, you can then call system.dataset.fromCSV() on the file contents to have it converted into a dataset for you when importing it. Much simpler.

I would also include the source tag path in the dataset that you are exporting to a CSV. How else are you supposed to be able to know what value to stick where? If you do that, you can loop through the dataset to create the list of tags to write to and their value.

One minor thing, don't import time or datetime, use Ignition's built in system.date functions.

Also imports should always go at the top of the project library above any other definitions or calls. If these methods are not in a project library they should be.

Cleaned Example
logger = system.util.getLogger("DAS.Config")

CONFIG_TAG_PATHS = [
	"[Beckhoff CTU]Devices/BOP Pressure",
	"[Beckhoff CTU]Devices/Chain Front Stretch",
	"[Beckhoff CTU]Devices/Casing Pressure"
]
CONFIG_HEADERS = [
	'tagPath',
	'value'
]

def exportDASConfigCSV(filePath=None):
	""" Exports a CSV dataset of the DAS configuration tags to the given filepath """
	
	if not filePath:
		filePath = "C:/CSV Files/%s/DAS_Config.csv" % system.date.format(system.date.now(), "yyyy-MM-ddTHHmmss")

	tagValues = [qv.value for qv in system.tag.readBlocking(CONFIG_TAG_PATHS)]

	configValueDataset = system.dataset.toDataSet(
		CONFIG_HEADERS,
		[[value] for value in tagValues]
	)

	system.file.writeFile(filePath, system.dataset.toCSV(configValueDataset), append=0)

	return


def importConfigCSV(filePath):
	""" Import DAS config CSV file and write values to tags """
	
	if not system.file.fileExists(filePath):
		logger.warnf("File '%s' does not exist or the system does not have permission to access it", filePath)
		return 0

	configFileData = system.file.readFileAsString(filePath)
	
	configData = system.dataset.toPyDataSet(
		system.dataset.fromCSV(configFileData))
	tagPaths, values = [], []
	
	for row in configData:
		tagPaths.append(row['tagPath'])
		values.append([row['value']])

	system.tag.writeBlocking(tagPaths, values)

	return 1
1 Like