Gadgets-or-Bust

Is there a Javascript equivilant for the php function fgetcsv

If so what is it and could you give an example.

Thanks

Gadgets-Or-Bust



Re: Sidebar Gadget Development Javascript Question

ToddOs

Assuming not everybody here is familiar with PHP, here's the definition of fgetcsv().

There is no direct analog of fgetcsv functionality in Javascript, but you do have some options:

  • If your file is local, you can use a FileSystemObject activex object to read the contents of a file and have a helper function to tokenize on commas (string's split() method should do that well enough).
  • If your file is remote, you can use XMLHttpRequest to retrieve the file and use the text of the result. In this case you'll get the whole file as a string rather than parsing through it line by line, so you'll have to split by '\n' first to find each line and then split again by ',' to tokenize each line.




Re: Sidebar Gadget Development Javascript Question

Andy E

It may be easier to parse the CSV into a 2D array using a regular expression. I'm rubbish at regular expressions, so I googled for the code and was shocked to find nothing for Javascript at all. So, I attempted it myself (and succeeded!):

Code Snippet

function ParseCSV(csvString)
{
csvString=csvString.replace(/"/g,"\\\""); // escapes any quotations already in the string
csvString=csvString.replace(/,/g, '","'); // replace all commas with ","
csvString=csvString.replace(/^(.+)$/gm,'["$1"],'); // add [" to the start of each line and "], to the end
csvString=csvString.substr(0,csvString.length-1); // remove the last , as this would cause an error

csvString="["+csvString+"]"; // Finally, add [ to the start and ] at the end, resulting in a JSON string
return eval("("+ csvString + ")");
}

csvArray = ParseCSV("Hello,Hello2,Hello3,Hello4\r\nHello,Hello2,Hello3,Hello4\r\nHello,Hello2,Hello3,Hello4");


I'm sure one of the more seasoned Javascript developers on here could write that better, but like I said, my regexp is rubbish.

Hope that helps,

Andy







Re: Sidebar Gadget Development Javascript Question

ToddOs

I still say it's easier and less expensive to use split. Using a RegEx for this is massive overkill. Pseudocode (that is probably real javascript but I'm not going to test it):

Code Snippet

function ParseCsv(var textData)

{

var lines = textData.split("\n");

var output = new Array(lines.length);

for (var i = 0; i < lines.length; i++)

{

output[i] = lines[i].split(",");

}

return output;

}

Much easier to understand, and because split only needs to walk forward on the strings the complexity is O(n) where n is the length of your textData input. A regex is great if you're doing some complex parsing. For splitting fields using well-known delimiters, a regex is overkill.




Re: Sidebar Gadget Development Javascript Question

Andy E

So RegExp uses more processing power I would have thought, especially on a large file, that RegExp would be quicker than looping, but I'm still a bit of a novice in terms of what's better for performance in js.

Andy






Re: Sidebar Gadget Development Javascript Question

ToddOs

It really depends on what you're doing. Regular expression operations are greedy (unless marked otherwise), which often requires a lot of backtracking. In a straight forward split situation like we have here, any backtracking will result in a slower parse than simply splitting the string (a forward-only walk of the string).

Regular expressions are great when you're doing complex matching. They're overkill when you want to do something simple like this. That said, I haven't benchmarked either one of these solutions. Honestly, I suspect they'd run in approximately the same amount of time, with the split solution being somewhat faster. It's possible the regular expression version might be a bit faster, but I doubt it. Even if that is the case, in this case I'd prefer clarity over performance.





Re: Sidebar Gadget Development Javascript Question

Andy E

Cool, thanks for clearing that up Smile






Re: Sidebar Gadget Development Javascript Question

Gadgets-or-Bust

The split function did the trick. Thanks !!!