Re: How to avoid stumbling over empty lines in csv data loader

Daniel Dekany <[email protected]> Thu, 2 Mar 2006 10:53:20 +0100
Newsgroups gmane.comp.text.fmpp
Message-ID <[email protected]>
Thursday, March 2, 2006, 8:58:32 AM, Ralf Hauser wrote:


> Daniel,
>
> Thx for the hint.
>> >> > The big problem with that is that empty lines are completely legal in
>> >> > csv files. Not only they are legal, but they are in fact used by MS
>> >> > Excel. They store rows where you have left all cells empty.
>> > Just tested that with an interesting outcome. When inserting an
>> > empty line, I still get the same error message referencing the
>> > trailing empty lines, but the list already fails at the first empty
>> line..
>> 
>> Of course it fails on the first empty line. Your FTL scripts fails on
>> the first empty cell that it reaches, as you have not written it to
>> handle empty cells.
> Ok, please see below an attempt to do this.

No no no... I told this so you understand why your script stops with
error (do you understand that anyway?). But you don't have to handle
empty cells, as they shouldn't occur in your application. As far as I
understand, it's the generated csv file what is bad, not the FTL that
later processes that and fails. So it would be better (cleaner) if
simply, your FTL script that generates the csv file doesn't produce
needless empty lines. Why not fix that instead?

> While as per http://freemarker.org/docs/ref_directive_list.html,
> there is a <#break>, there doesn't appear to be a <#continue>

There is no "continue" directive. But if you want to skip a
loop-cycle, you can put the whole cycle into an if, as you did
(although I don't see why is the introduction of "continue" variable
needed). But I don't think you need this... see above.

> But perhaps I don't need that. Do you think there is any
> improvement potential to the below?
>
> Regards
>
>    Ralf
>
> <#list n_xml as d>
> <#if d?exists>
> <#list n_xml.headers as x><#if
>    d[x]?exists><#assign continue="true"><#else
>    ><#assign continue="false"></#if></#list>
>    <#if continue="true">
>  INSERT INTO  ${tbl} (<#list n_xml.headers as x>${x}<#if 
>    x_has_next>,</#if></#list>)  VALUES (<#list n_xml.headers as x>'<#if d[x]?exists>${d[x]}<#else
>    ><@pp.warning message = "skipping empty cell ${x_index}" /> </#if>'<#if
>    x_has_next>,</#if></#list>);
> <#else>
>   <@pp.warning message = "skipping empty line0 ${d_index}" />
> </#if>
> <#else><@pp.warning message = "skipping empty line1 ${d_index}" /></#if>
> </#list>
>


-- 
Best regards,
 Daniel Dekany



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642