I am executing a script which is converting table to json script and then further into csv. During this process the Json contains degree Celsius in \xb0c format and upon csv download it comes as ° C.
You’re going to have to give us more information, eg your code, in order for us to know what you're doing to be able to help
Without code I'm going to call my shot and say that you need to use the u prefix to designate your string literals as unicode text rather than ASCII, because Python 2.
But yeah, share your code.
try:
data = self.page.props.data
headers = [
'ID','ParameterName','RP','SLNo',
'Unit','AlarmMinimum','SetPointMin','SetPoint','SeTPointMax',
'AlarmMaximum',
'RParameterType','DataType'
]
def to_unicode(val):
if val is None:
return u""
if isinstance(val, unicode):
return val
if isinstance(val, str):
# Decode legacy byte strings correctly
return val.decode('latin-1') # or 'cp1252'
return unicode(val)
rows = [[to_unicode(row.get(h, "")) for h in headers] for row in data]
ds = system.dataset.toDataSet(headers, rows)
# Convert dataset to CSV (Ignition returns Unicode text)
csv_text = system.dataset.toCSV(ds)
# Encode ONCE to UTF‑8
csv_bytes = csv_text.encode('utf-8')
system.perspective.download(
'Recipe_{}_Excel.csv'.format(self.view.params.workcenterCode),
csv_bytes,
'text/csv; charset=utf-8'
)
except Exception as e:
import traceback
logger.error(traceback.format_exc())
Dangit, I was going to call my second shot but missed out.
Here's the line: Don't blindly trust LLMs for help coding in Ignition. They're really bad at it, because they lack the specific context of how to write good code for Ignition.
You don't need to do anything related to encoding. Your LLM led you directly up a blind alley and gave you ~double the amount of code you actually need to solve this problem:
headers = [
'ID',
'ParameterName',
'RP',
'SLNo',
'Unit',
'AlarmMinimum',
'SetPointMin',
'SetPoint',
'SeTPointMax',
'AlarmMaximum',
'RParameterType',
'DataType',
]
rows = [[row.get(h, "") for h in headers] for row in self.page.props.data]
ds = system.dataset.toDataSet(headers, rows)
csv_text = system.dataset.toCSV(ds)
system.perspective.download(
'Recipe_{}_Excel.csv'.format(self.view.params.workcenterCode),
csv_bytes,
'text/csv; charset=utf-8'
)