Change Date/Time data type for csv file

I am running ignition edge and once a day I am creating a csv file with floating point values coming from the tag historian. I have everything working perfect except my date is being shown as a [u'] type instead of just a string which I want it to be.

Here is a screenshot of the csv file and also the script I have to create the file:

Here is my script which creates the csv file:

def create_analogin_csv():
	"""Create analog input csv file with values from past 24 hours
	
	Params:
		none
	
	Returns:
		none
	"""

	#establish logger
	logger = system.util.getLogger("AI CSV file")

	#dynamically create list of analog input tag paths including filter to only look at UDT instance tags
	path = '[PLC]AnalogInputs'
	fltr = {'tagType': "UdtInstance"}
	tags = system.tag.browse(path,fltr)
	dataTagPaths = [str(tag['fullPath']) + "/InputValue" for tag in tags]

	#create custom header row
	headers = []
	headerTagPaths = [str(tag['fullPath']) + "/DeviceName" for tag in tags]
	  #reads in device names and creates array for header.  Inserts DATE/TIME header in first index position
	headers = 'DATE/TIME' + "," 
	
	for header in system.tag.readBlocking(headerTagPaths):
		headers = headers + str(header.value) + ","

	#execute tag history query returning hourly values for last 24 hours
	AIdataset = system.tag.queryTagHistory(paths=dataTagPaths, returnSize=24,
		aggregationMode="LastValue", returnFormat='Wide', columnNames=headers, rangeHours = -24)

	#write data to dataset tag
	AIdatasetTag = '[PLC]AnalogInputCSV'
	system.tag.writeBlocking(AIdatasetTag, AIdataset)
	logger.info("Data written to dataset tag")

	#take resulting dataset and format time and date
	AIdatasetF = system.dataset.formatDates(AIdataset, "MM-dd-yyyy HH:mm")
	
	#format dataset to one decimal place and convert dataset to csv
	outputRows = [headers]
	for row in xrange(AIdatasetF.rowCount):
		# add the date value
		outputRow = [AIdatasetF.getValueAt(row, 0)]
		outputRow = str(outputRow) + ","
		# skip the date column
		for col in xrange(1, AIdatasetF.columnCount):
			outputRow = outputRow + str(round(AIdatasetF.getValueAt(row, col), 1)) + ","
		outputRows.append(outputRow)
	
	csv = "\n".join((row for row in outputRows))
	
	#convert dataset to csv file
	#csv = system.dataset.toCSV(dataset = AIdatasetF, showHeaders = True)

	#define file path and populate file
	filePath = "C:\\DailyCSVFiles\\analogin.csv"
	system.file.writeFile(filePath, csv)

	if system.file.fileExists("C:\\DailyCSVFiles\\analogin.csv"):
		logger.info("File written to csv")
	else:
		logger.info("File does not exist!")

Looks like you're already converting the data to a string, so it would be best to cut off the first three and last three characters if the data is a date here:

outputRow = str(outputRow) + ",".

1 Like

It looks like your date is coming through as a list or other iterable object, which is why the str() call for the date is formatting it with square braces, etc. I don't know...

If it is a list, you might try adding an index like the following to pull out the first value:

outputRow = str(outputRow[0]) + ","

HTH.
-Shane

1 Like