How to create and append data to a csv/txt file. - Forum

Forum Navigation
You need to log in to create posts and topics.

How to create and append data to a csv/txt file.

Hi guys, I've searched high and low through the documentation and am struggling to find how to create a csv file and append data to the file. I assume this would be done by creating an array and then writing the array to the file. I hate having to ask as I always try to work things out for myself, but assistance would be greatly appreciated.

Many thanks

Rob

@rob-maggs

Good time! A CSV file is a simple text file whose data mimic a table. This means that each line of the file is a single table row, and the values of the table fields (columns) are separated by commas (or tabulation mark, or other delimiter). You can write data to such a file like to any other text file (e.g. .txt).

An example of the contents of such a file with a table of three rows and three columns:

Year, Name, City
1975, Vadim, Tyumen
1965, Andrey, Ivanovo

Rob Maggs has reacted to this post.
Rob Maggs

Thanks for the reply @vadim, I understand what a CSV file is, I just want to know how I can take a line of data and write it to a text file, and then append more data sequentially. So for example I create a string as a variable, write that variable to a text file...and then append lines, sorry maybe I did not descrive what I'm trying to do very well :)

@rob-maggs

The general idea is this (an example of writing one string with three variables):

SetVar "[Year]" "1975"
SetVar "[Name]" "Vadim"
SetVar "[City]" "Tyumen"
FileWrite "[PubDir]Test.csv" "Append" "[Year],[Name],[City]"

Or so:

SetVar "[var]" "1975, Vadim, Tyumen"

FileWrite "[PubDir]Test.csv" "Append" "[var]"

luishp, Darbdenral and Rob Maggs have reacted to this post.
luishpDarbdenralRob Maggs

Thank you @vadim, that is exactly what I needed. Much appreciated.

Sorry to be a pain, but where do I find "FileWrite" in the web version?

I think @vadim was thinking of VISUALNEO WIN.

Hey guys, @vadim any update on this or link to a resource that will help me, many thanks. I see I can write an initial variable to file by using:

VarToLocalFile [comment] "comment.txt"

But how can I append sequential variables to the file created?

Many thanks Rob

fkapnist has reacted to this post.
fkapnist

@rob-maggs,

There's more then one way to do this..

I wasn't sure if you mean update values or append the whole new record.. so

Here's a few samples I believe would work.

First a simple update that initially changes the first value.  Then we expand upon this to change all values too.  Then add whole new line to CSV.  I think you can see how to add new values now?

Simple approach updating values:

. could be variables here
SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4"
. add item
SetVar [csv] "[csv],ITEM5"
CreateEmptyArray [csvArr]
StrParse "[csv]" "," [csvArr]
ArrayLen [csvArr] [totalRecords]
If [totalrecords] != 0
  jsAlert "[csvArr]"
  .now save your CSV to disk
  VarToLocalFile [csvArr] "myfile.txt"
Else
  AlertBox "ERROR: " "No Records!" ""
EndIf

 

Expanded approach updating values:

. could be variables here
SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4"
CreateEmptyArray [csvArr]
StrParse "[csv]" "," [csvArr]
jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
ArrayLen [csvArr] [totalRecords]
If [totalrecords] != 0
  .change specific value
  SetVar [csvArr(0)] "NEW VALUE{0}"
  jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
  Loop 0 [totalRecords -1] [num]
    If [num] = 0
      .change specific value
      .SetVar [csvArr(0)] "NEW VALUE{[num]}"
      .jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
    EndIf
  .change all values
  SetVar [csvArr([num])] "NEW VALUE{[num]}"
  EndLoop
jsAlert "CSV = [csvArr(0)], [csvArr(1)], [csvArr(2)], [csvArr(3)]"
  .now save your CSV to disk
VarToLocalFile [csvArr] "myfile.txt"
Else
  AlertBox "ERROR: " "No Records!" ""
EndIf

Append a new record and save to file:

. could be variables here
SetVar [csv] "ITEM1,ITEM2,ITEM3,ITEM4"
. add new record
SetVar [csv_new] "[csv]\nITEM5,ITEM6,ITEM7,ITEM8"

CreateEmptyArray [csvArr]
StrParse "[csv_new]" "," [csvArr]
ArrayLen [csvArr] [totalRecords]
If [totalrecords] != 0
  jsAlert "[csvArr]"
  .now save your CSV to disk
  VarToLocalFile [csvArr] "myfile.txt"
Else
  AlertBox "ERROR: " "No Records!" ""
EndIf

 

luishp, Vadim and 2 other users have reacted to this post.
luishpVadimfkapnistRob Maggs

@rob-maggs

Here is a sample pub that might help you.

Load File. Append File. Save File.

Then Load File again to see the changes...

https://www.awareware.com/NEOWINPLUGINS/AppendData.zip

Vadim, Darbdenral and Rob Maggs have reacted to this post.
VadimDarbdenralRob Maggs

Many thanks Guys, much appreciated :)

Darbdenral has reacted to this post.
Darbdenral

@rob-maggs

Yes, sorry, I meant VisualNEO Win, of course.