Forum

Using Python in external scripts
Contributor
Offline
User avatar
Posts: 21
Joined: Mon Oct 07, 2013 12:15 pm

Double quoting results and changing delimiters

by ChakLeung » Mon Dec 08, 2014 12:08 pm

Sometimes of your analytical results may need to be sent back to Kognitio as strings (such as variable names, type of results etc.). There may be a scenario where there is a conflict with your choice of delimiter. For example you may wish to write out some variable names as a single column:
DIST_TRAVELLED
DIST_MILES[0,53.2]
DIST_MILES(53.2,106]
DIST_MILES(106,160]
DIST_MILES(160,213]
DIST_MILES(213,266]
This would cause a problem since the default delimiter in Kognitio as a comma. Thus Kognitio would interpret it like this:
DIST_TRAVLLED , Undefined_Column
DIST_MILES[0 , 53.2]
DIST_MILES(53.2, 106]
DIST_MILES(106, 160]
DIST_MILES(160, 213]
DIST_MILES(213, 266]
i.e. as two separate columns because of the comma within the variable names when in fact they should be one column (this would cause an invalid external data record error since the number of columns being sent back and the ones defined in the external script configuration would not match).
This can be avoided by changing the delimiter from comma to something else such as dashes, hyphens or carets for example. This can be defined in the external script configuration after the "sends" configuration using the following:
Output ‘fmt_field_separator ^’
Which changes the delimiter to a caret and will no longer recognise the comma in the variable name as a delimiter.
Whilst this is useful for testing purposes, some functions in R and Python use the comma delimiter by default. An alternative to this is to wrap the troubling variable names in double quotes like so:
DIST_TRAVELLED
“DIST_MILES[0,53.2]”
“DIST_MILES(53.2,106]”
“DIST_MILES(106,160]”
“DIST_MILES(160,213]”
“DIST_MILES(213,266]”
Kognitio would then be able to interpret this as one single column since the comma is now being recognised as part of the string and not a delimiter.
In Python, there isn’t a known function to do this like in R but wrapping the text variable in double quotes achieves the same result:
csvwriter.writerow([‘”’ + variable_name + ’”’])
where csvwriter is your defined standard output stream. An alternative is to use the notation below which experience programmers might find more visually appealing:
csvwriter.writerow([‘”%s”’ %(variable_name)])
Reply with quote Top

Who is online

Users browsing this forum: No registered users and 1 guest

cron