Can you add a column to a csv file?

School Boy Error

Member
Licensed User
Longtime User
I've been toying with this bit of code:

B4X:
   Dim headers As List
      List1 = StringUtils1.LoadCSV2(Dir, Filename, ",", headers)
      ' Sets the header names of the columns 
      Dim h(headers.Size) As String
      For i = 0 To headers.Size - 1
         h(i) = headers.Get(i)
      Next

I've tried editing it to:

B4X:
For i = 0 to header.size ' to allow for the new column
If i = headers.size - 1 then
h(i) = "" 'to add a blank column one before the last column
Else
h(i) = headers.get(i)
End if
Next

My question is how can I add a column into a csv? (If that is possible)
 

stevel05

Expert
Licensed User
Longtime User
It looks to me as if what you really are trying to do is to add a column to the imported csv file.

Using the LOADCSV2 you actually end up with two objects to manipulate, the headers which is a list, and the data which is a list of arrays. So you need to insert data into both objects in the correct places in the row.

And you need to add data to the relevant place for each data row.

Have a look at the attached project which achieves this on a simple level.

It depends on what you are trying to do as to whether this is actually worth doing, it may be easier to change the source csv, and if there are going to be lots of changes. it may be better to convert the imported data in to a different data structure or database.
 

Attachments

  • csvtest.zip
    5.9 KB · Views: 267
Last edited:
Upvote 0
Top