0

I am trying to read a csv file that is using a | delimiter instead of ,.

I am using

<cffunction name="loadCSVfile" access="public" returntype="string"> 
    <cfargument name="filename" required="yes" default="">

    <cffile action="read" file="#filename#" variable="csvData">

    <cfreturn csvData>
</cffunction>

which works fine with files that are , delimiter but does not work with |. Is there anything else that I need to do for this function to work?

This is how I'm calling the function:

    <cfscript>
       csvloaderCFC = CreateObject("component","AlscCSVloader");
    </cfscript>


    <cfset csvfilename = DataDirectory&q_DataDirectory.name>

    <!---  backup CSV file ---->
    <cffile action= "copy" source = "#csvfilename#"  
                destination = "#DataDirectorybackup#"> 

    <!---  Load CSV data ---->
    <cfscript>
        csvData = csvloaderCFC.loadCSVfile(csvfilename);
    </cfscript>

    <cfdump var="#csvData#">

The cfdump in the end it doesn't show at all

Pipe-delimitted sample

Location|patient_ID|First Name|Last Name|
111|468454|Eric |Gallegos |
111|468457|kelsey|anderson|
10|468459|Rivenda|Kaur|

Comma-delimitted sample

Location,patient_ID,First Name,Last Name,
111,468454,Eric ,Gallegos ,
111,468457,kelsey,anderson,
10,468459,Rivenda,Kaur,
Leigh
  • 28,765
  • 10
  • 55
  • 103
Geo
  • 3,160
  • 6
  • 41
  • 82
  • 2
    Well it's not a CSV file if it doesn't use commas (the C in CSV stands for comma... ;-). But what do you mean "does not work". Does not work in what way? What does it do instead of working? What's your benchmark for deciding it does/doesn't work? All you're doing here is reading a file... there's nothing there that even *cares* if it's a CSV or pipe-delimited or anything. All that code does is read the file and put the contents into a string. You should VAR `csvData`, btw. – Adam Cameron Jan 22 '13 at 22:12
  • It does not work at all and it does not show any errors either. What do you mean Var `csvData`? – Geo Jan 22 '13 at 22:37
  • VAR: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7dfb.html#WSc3ff6d0ea77859461172e0811cbec0ac4a-7fbd. – Adam Cameron Jan 22 '13 at 22:44
  • 2
    Before you can determine that code "doesn't work", you need to tell us your expectations of "does work". That code is just a function. It in and of itself won't *do* anything. You need to call it. Even then all it does is read the file and return its contents so - like I said - commas... pipes... completely irrelevant as far as that code is concerned. What constitutes "works fine" with files that are comma-delimited but "does not work" with pipe-delimited ones? – Adam Cameron Jan 22 '13 at 22:48
  • 2
    **Saying "it does not work" is completely useless.** You either have an error message, or you have behaviour that is different to what you expect. Without that information (the error message or the difference between what you expect and what is occuring) we cannot help you. – Peter Boughton Jan 22 '13 at 22:52
  • When I am trying to read a comma-delimited file the code outputs the content of the file as it should. When I am trying to read a pipe-delimited file with the same code i get nothing, no output and no error message. @PeterBoughton I hope this helps a bit more. I will update my question to include the rest of the code where I'm calling the function – Geo Jan 22 '13 at 22:58
  • 2
    Pls provide sample data files for both CSV and pipe-delimited files too. Basically create a stand-alone reproduction case of what you are seeing (with no dependencies except the data files), and nothing omitted. But the bare minimum code that demonstrates what you're seeing. – Adam Cameron Jan 22 '13 at 23:00
  • The file, CSV or not, should be read correctly. Make sure you var-scope (or local scope) your `csvData`. – Henry Jan 22 '13 at 23:10
  • 4
    Another tangential thing I just noticed here. An argument is either *required* or it has a default. Not both. This is not a syntax error (hence the code compiles), but it's a logic error. This will not impact your issue here though. – Adam Cameron Jan 22 '13 at 23:16
  • The code provided thus far does not act differently for commas or pipes. (It also still does not do significantly more than just output the raw file contents.) If you are experiencing different behaviour, you have not provided all the relevant code. (Or perhaps are looking at a different file?) – Peter Boughton Jan 22 '13 at 23:26
  • @PeterBoughton I just wanted to output the raw file contents, nothing else. I am really sorry for not providing enough but this is all I have. I do not have actual error output. – Geo Jan 22 '13 at 23:29
  • @AdamCameron Thanks for the catch, i removed the default part. – Geo Jan 22 '13 at 23:31
  • Another little detail, you need to var the variables inside your function. In your case, it's the variable named csv data. You might also want to edit your question to show the removal of the default part. Also, dump early and dump often. Inside your function dump the arguments scope to make sure they are what you expect. Then dump the variable inside the function after you attempt to read the file. – Dan Bracuk Jan 23 '13 at 00:01
  • Aside from the logic errors others have mentioned (and some style issues) the code above works fine with the data samples above. It dumps the contents of whatever file you pass in (if the file exists). So either your actual code/data is different or there is more to it than what you have posted .. For example what is the source (and value) of the variables:`DataDirectory`, `q_DataDirectory.name`, etcetera? The `q` prefix suggests a query or loop elsewhere in the code .. Is there a `try/catch` or error handler that might be obscuring any errors? – Leigh Jan 23 '13 at 01:54
  • Connected to what @Leigh said... what you've given us is not a repro case, it's just a section of your code. As per my earlier request: pls give us a stand-alone self-contained minimal repro case that has no clutter (external CFCs, variables we don't know the source of, etc), just code that demonstrates what you're seeing. This should be part of your troubleshooting process anyhow: factor out all the elements that are irrelevant to the problem, leaving just a minimal example demonstrating your problem. – Adam Cameron Jan 23 '13 at 06:58
  • First, thanks to everyone who is trying to help so far. I just thought about another issue that it may causing this "alien" behavior. Is there a chance for the file to be too big to parse at once? – Geo Jan 23 '13 at 15:42
  • 1
    And yes, that was my issue. The file that I was trying to process was too big.... – Geo Jan 23 '13 at 16:08
  • 1
    Glad you figured it out. Based on your updates, it sounds like the problem was an `OutOfMemory`? Severe (unrecoverable) errors do not always appear on screen. Always check the log files too! – Leigh Jan 23 '13 at 19:29

1 Answers1

0

There is quite a bit more you have do do than just a CFFile call to get a CSV as a struct of some kind. CFFile foes not do this on it's own. (although strangely CFHttp does) Honestly your best bet may be to be to use a Java library. There are several available from Apache: http://commons.apache.org/csv/

These should give you back something fairly easy to deal with.

If you want a "pure" coldfusion CSV option you can look at this blog post for a specific impl: http://www.bennadel.com/blog/483-Parsing-CSV-Data-Using-ColdFusion.htm

basically you need to read the entire content and then parse it.

ryber
  • 4,537
  • 2
  • 26
  • 50