Skip to main content
Inspiring
January 23, 2007
Answered

How to loop through CSV file

  • January 23, 2007
  • 2 replies
  • 4508 views
Hi,

I want to loop through each column of a CSV file.
And while looping each column, I want to loop each row starting with the 2st row.

I wrote the code as below, but it is looping through the rows starting from row 1.
How do I make it start the loop as from row 2 in the CSV ?
(see code below)

Can someone tell me how to do this?

Thx




<!--- get and read the CSV-TXT file --->
<cffile action="read" file="C:\Documents and Settings\user\Desktop\EM-CSV\test.csv" variable="csvfile">

<!--- loop through the CSV-TXT file on line breaks and insert into database --->
<table border="1">


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

<!--- <cfoutput>#listgetAt('#index#',1, ',')#</cfoutput><br> --->
<tr>
<td><cfoutput>#index#</cfoutput></td>
<td><cfoutput>#listgetAt('#index#',2, ',')#</cfoutput></td>
<td><cfoutput>#listgetAt('#index#',3, ',')#</cfoutput></td>
</tr>

</cfloop>
</table>
    This topic has been closed for replies.
    Correct answer BKBK
    What error message did you get? Was it perhaps about an empty row? If so, here


    Hi Like2Flex,

    The problem is that coldfusion doesn't see

    list="a,b,,,,,c,,d"

    as a list of 9 elements as you would expect. It sees just the 4 elements a,b,c,d because Coldfusion doesn't include two delimiters next to each other.

    One solution to the problem is to manually insert a space character between two delimiters that are next to each other. Here is an example of such a solution

    <cfset list="a,b,,,,,c,,d">
    <cfset list=replace(list,",,",", ,","all")>
    <cfset list=replace(list,",,",", ,","all")>
    list length: <cfoutput>#listlen(list)#</cfoutput>



    2 replies

    Inspiring
    January 24, 2007
    I have not tried it myself, but theoretically you can do a Replace(row, ",,", ", ,") first, adding a space (or whatever you want, even your "It is blank" phrase) between consecutive commas in the list so that the list item will not be blank and will be picked up by the list functions...
    Like2FlexAuthor
    Inspiring
    January 24, 2007
    Hello,

    I did :
    <cfoutput>#Replace(row, ",,", ", ,", "ALL")#</cfoutput>
    Replace(row, ",,", ", ,")
    BKBK
    Community Expert
    Community Expert
    January 23, 2007
    It often helps to use a suggestive name like row.


    Like2FlexAuthor
    Inspiring
    January 23, 2007
    Hello, your code works perfectly.
    Now I am having a slight problem with it.
    I have added the code to check whether a particular cell of the CSV is blank. (see code below)

    But it is giving me error message.

    Can you please tell me what am doing wrong?

    Thx

    <!--- loop through the CSV-TXT file on line breaks and insert into database --->

    <cfset isRowTwoOrLater=false>
    <table border="1">
    <cfloop index="row" list="#csvfile#" delimiters="#chr(10)##chr(13)#">
    <cfif isRowTwoOrLater>
    <tr>
    <cfif #listgetAt(row,1, ',')# neq ''> <!--- I HAVE ADDED THIS PART TO CHECK WHETHER IT IS BLANK --->
    <td><cfoutput>#listgetAt(row,1, ',')#</cfoutput></td>
    <cfelse>
    <td><cfoutput>It is blank</cfoutput></td>
    </cfif>

    <td><cfoutput>#listgetAt(row,2, ',')#</cfoutput></td>
    <td><cfoutput>#listgetAt(row,3, ',')#</cfoutput></td>
    </tr>
    <cfelse>
    <cfset isRowTwoOrLater = true>
    </cfif>


    </cfloop>
    </table>
    BKBK
    Community Expert
    Community Expert
    January 23, 2007
    What error message did you get? Was it perhaps about an empty row? If so, here