Skip to main content
Inspiring
January 9, 2017
Question

Need help with csv read.

  • January 9, 2017
  • 2 replies
  • 1030 views

I have some csv files that are in this format:

"Source","Requester ID","ID","Start Date","End Date","location","Value","TimeStamp"

"DCA","tedkrasav","9913457","12/4/2016 12:00:00 AM","12/18/2016 12:00:00 AM","Room_121",,"12/3/2016 2:50:03 PM"

What I am trying to do is read the file and insert all of the individual columns into a DB table.

I am trying to do this method just to see the individual columns but I keep getting an error that states "Invalid list index 2.In function ListGetAt(list, index [, delimiters]), the value of index, 2, is not a valid as the first argument (this list has 1 elements). Valid indexes are in the range 1 through the number of elements in the list" :

<cffile action="read" file="csvdemo.csv" variable="csvfile">


<cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">

<cfoutput>

#listgetAt('#index#',1, ',')# #listgetAt('#index#',2, ',')# #listgetAt('#index#',3, ',')# #listgetAt('#index#',4, ',')#

</cfoutput>
</cfloop>

Does anyone know what I am doing wrong?




This topic has been closed for replies.

2 replies

Legend
January 11, 2017

There is a java csv reader that may make this easier for you. I'm on the road and I don't have access to example code but you could google "coldfusion java csv" and you'll probably find examples.

WolfShade
Legend
January 9, 2017

My assessment could be wrong, but I believe that the delimiter should be #chr(13)##chr(10)#, not the other way around.

HTH,

^_^

siriivenAuthor
Inspiring
January 9, 2017

I changed that around but I still received the same error.

WolfShade
Legend
January 9, 2017

Odd.  Okay, try this.  Set the delimiter to #chr(13) & chr(10)# and see if that fixes it.

HTH,

^_^