# Convert dataset to string

**URL:** <https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903>\
**Category:** Ignition\
**Created:** [June 7, 2010, 10:11am UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903 "2010-06-07T10:11:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [June 7, 2010, 10:11am UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/1 "2010-06-07T10:11:54Z")

</div>

I’m trying to convert a Python dataset (returned from a SQL query) into a string of comma separated variables. The following code works:`data = system.db.runQuery("SELECT Number FROM MyTable")
for row in data:
   csvString = csvString + row[0] + ","
#Remove the final comma.
csvString = csvString[:-1]`Is there a more elegant ‘Pythonesque’ way of doing this? I tried using ‘join’, but this only works on lists, not on datasets.

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [June 7, 2010, 1:40pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/2 "2010-06-07T13:40:27Z")

</div>

A couple of ideas—

1. I tend to use += when building strings like this, i.e. csvString += row[0] + “,”. I’m not sure if it runs faster, but it’s less typing and looks better.

2. You could concatenate Number with a comma in the query so the loop instruction was just csvString+=row[0].

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [June 7, 2010, 2:07pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/3 "2010-06-07T14:07:59Z")

</div>

What database are you using? You may be able to do the formatting in the query. You could also have the RDBMS system do it (ie T-Code in MSSQL) instead of the Ignition gateway or client.

For example, the [MySQL Manual SELECT](http://dev.mysql.com/doc/refman/5.0/en/select.html) gives an example that outputs a CSV file. I suspect that the following code could be used without the “INTO OUTFILE” portion of the query.

`SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;`

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [June 7, 2010, 2:23pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/4 "2010-06-07T14:23:04Z")

</div>

Thanks for the ideas. What I’m trying to do is convert data from a dataset into a list of comma separated variables in a string e.g. 1,2,3 so that I can then use the string in another SQL query, like`UPDATE MyTable SET Status='Complete' WHERE ID IN (1,2,3) `I know I could embed the first query in the second as a sub-query, but I also need to use the IDs (1,2,3 in the above example) to create new records in another table. For this I need to create the SQL query by iterating through the dataset.

I just wondered if there was a ‘magic’ Python one-liner which would translate data in a dataset into a csv string. What I’ve got works, but unless I push the boundaries with Python I’m not going to expand my limited knowledge 🙂

---

<div class="post-metadata">

**Author:** ![ISDProj](https://avatars.discourse-cdn.com/v4/letter/i/58956e/32.png) [@ISDProj](https://forum.inductiveautomation.com/u/ISDProj)\
**Post date:** [June 7, 2010, 9:46pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/5 "2010-06-07T21:46:43Z")

</div>

Al,  
Try this:

```auto
groupConcat({Root Container.Table.data}, "FieldName", ", ")
```

I use it frequently in Dynamic Properties. (it is described in the Ignition Manual under “Aggregates”)

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [June 8, 2010, 7:07am UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/6 "2010-06-08T07:07:58Z")

</div>

ISDProj,

That is exactly what I need, except it has to be a Python equivalent to run in a script, rather than an expression function.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [June 14, 2010, 5:52pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/7 "2010-06-14T17:52:15Z")

</div>

Can’t you simply use [system.dataset.dataSetToCSV()](http://www.inductiveautomation.com/support/usermanuals/ignition/index.html?system_dataset_datasettocsv.htm)?

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [June 14, 2010, 6:00pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/8 "2010-06-14T18:00:20Z")

</div>

When did you sneak that function in there ? 😆

That certainly fits the bill - it’s a pity there isn’t a PyDataSet version as well.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [June 14, 2010, 8:44pm UTC](https://forum.inductiveautomation.com/t/convert-dataset-to-string/1903/9 "2010-06-14T20:44:00Z")

</div>

I think it has been in Ignition all along, but it wasn’t in FactoryPMI. You can convert a PyDataSet to a DataSet quite readily.
