# Looking for new tricks for CSV file creation

**URL:** <https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633>\
**Category:** General\
**Created:** [July 22, 2021, 8:16pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633 "2021-07-22T20:16:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Reynolds](https://forum.xojo.com/user_avatar/forum.xojo.com/william_reynolds/32/565_2.png) [@William\_Reynolds](https://forum.xojo.com/u/William_Reynolds)\
**Post date:** [July 22, 2021, 8:16pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/1 "2021-07-22T20:16:38Z")

</div>

Given the XOJO language has expanded and improved over the past few years, I wonder if anyone has come up with a more clever way to building a .CSV file.

I have a pretty generic requirement, but if I can use this chance to update my skillset why not?! CSV files have a simple header, followed by rows, and escaping commas and whitespace by bracketing each entry in quotes… all pretty straight forward.

I need to create the entries programmatically, and the column order is important to maintain. I’ll be writing the result out to disk via a traditional BinaryStream.

If there are any built-in functions I’m overlooking be sure to embarrass me and point them out.

And have a great day! Thanks!

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 22, 2021, 8:22pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/2 "2021-07-22T20:22:53Z")

</div>

“More clever way” than what?

If you want your code improved, you obviously need to show what you are doing.

---

<div class="post-metadata">

**Author:** ![William\_Reynolds](https://forum.xojo.com/user_avatar/forum.xojo.com/william_reynolds/32/565_2.png) [@William\_Reynolds](https://forum.xojo.com/u/William_Reynolds)\
**Post date:** [July 22, 2021, 8:32pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/3 "2021-07-22T20:32:08Z")

</div>

Excellent point @Markus_Winter ! I am just starting to code the module, so the code I’d start writing would be something like…

1- Create a String array for Header values, and a String array for every row.  
2.- For each value in the header I’d check for offending characters, etc.  
3.- Sequentially append a large string variable with each header entry, plus a kComma character.  
4.- Insert a NewLine  
5.- Grab my first array of “values” and append them to the same growing string, plus a NewLine

…a somewhat “manual” way of constructing this big piece of text.

---

<div class="post-metadata">

**Author:** ![Douglas\_Handy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Douglas\_Handy](https://forum.xojo.com/u/Douglas_Handy)\
**Post date:** [July 22, 2021, 8:41pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/4 "2021-07-22T20:41:35Z")

</div>

> [@William\_Reynolds](#):
>
> Given the XOJO language has expanded and improved over the past few years, I wonder if anyone has come up with a more clever way to building a .CSV file.

I know of a few things to READ a csv file (the parsing is really not as straight forward as you may think, to do edge cases correctly). But I am not aware of any existing classes to WRITE a csv file.

My usual way to create any delimited file is to create a string array, add an entry for each “column”, and then Join the array with the delimiter to create a text line. I almost always output using a tab delimiter so I don’t have to mess with escaping.

So my generic WriteTabLine() method looks similar to this:

```auto
dim line as string= Join(flds,Chr(9)) + EndOfLine
fs.write line
return

```

where I just pass in the TextOutputStream (i.e. “fs” above) and the string array of fields to include (i.e. “flds” above).

I tend to just add lines to my output file as I go, including a header before I start looping over data. Then close out the file stream at the end. But you could also build that up in another string array (e.g. “lines()” or whatever) that you ultimately turn into a string again using join() but with an EndOfLine instead of tab or comma delimter.

For csv output, I’d probably do a similar WriteCsvLine() which before doing the join on the passed string array, loops over it and properly escapes each value.

Then in your code you can ignore all the complexities and just create a new “line” of output by clearing the array and appending data as needed.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 22, 2021, 9:00pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/5 "2021-07-22T21:00:26Z")

</div>

Might also be worth looking at Norman’s CSV Parser code:

> **[GitHub - npalardy/CSVParser: Event based and recordset like CSV parser for Xojo](https://github.com/npalardy/CSVParser)**
>
> Event based and recordset like CSV parser for Xojo - GitHub - npalardy/CSVParser: Event based and recordset like CSV parser for Xojo

---

<div class="post-metadata">

**Author:** ![Douglas\_Handy](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Douglas\_Handy](https://forum.xojo.com/u/Douglas_Handy)\
**Post date:** [July 22, 2021, 9:04pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/6 "2021-07-22T21:04:03Z")

</div>

> [@Markus\_Winter](#):
>
> Might also be worth looking at Norman’s CSV Parser code:
> 
> [GitHub - npalardy/CSVParser: Event based and recordset like CSV parser for Xojo](https://github.com/npalardy/CSVParser)

And that is one of the classes I was referring to help READ a CSV file, but I was not aware it may help create one. Since I have the MBS extensions, I tend to just it for parsing instead of Norman’s.

---

<div class="post-metadata">

**Author:** ![Robert\_Weaver](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@Robert\_Weaver](https://forum.xojo.com/u/Robert_Weaver)\
**Post date:** [July 22, 2021, 9:17pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/7 "2021-07-22T21:17:31Z")

</div>

Writing a CSV file is a lot easier than reading one. In the [Just Code Challenge](https://forum.xojo.com/t/just-code-challenge-submissions/45535), a few years ago, I submitted a simple CSV file editor that reads, edits and writes CSV files. The creation and writing part is very straightforward. The project can be downloaded here:  
[CSV File Editor Project](http://electronbunker.ca/XojoProjects/Week5_CSV_Editor.zip)

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 22, 2021, 9:44pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/8 "2021-07-22T21:44:55Z")

</div>

> [@Douglas\_Handy](#):
>
> but I was not aware it may help create one.

Since one is the reverse of the other it can show a few tricks either way.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 22, 2021, 10:07pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/9 "2021-07-22T22:07:14Z")

</div>

Use a TextOutputStream and WriteLine to write line by line. Building one long string with embedded EndOfLine is not a good approach.

Tab delimited format is much easier and is pretty ubiquitous.

---

<div class="post-metadata">

**Author:** ![William\_Reynolds](https://forum.xojo.com/user_avatar/forum.xojo.com/william_reynolds/32/565_2.png) [@William\_Reynolds](https://forum.xojo.com/u/William_Reynolds)\
**Post date:** [July 22, 2021, 10:23pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/10 "2021-07-22T22:23:19Z")

</div>

Thanks Tim, comma separated is the users requirement but I’ll heed your advice to iteratively writing it out 👍

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 22, 2021, 11:11pm UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/11 "2021-07-22T23:11:32Z")

</div>

> [@Tim\_Hare](#):
>
> WriteLine to write line by line

I would say reading / writing each line separately is not a good approach. But maybe that’s just me … 🙄

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 23, 2021, 3:49am UTC](https://forum.xojo.com/t/looking-for-new-tricks-for-csv-file-creation/64633/12 "2021-07-23T03:49:17Z")

</div>

Norman has just updated his CSV Parser …
