Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion CSV to array
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Fox  
View profile  
 More options Nov 27 2000, 11:39 pm
Newsgroups: comp.lang.javascript
From: Fox <maho...@eatel.net>
Date: Tue, 28 Nov 2000 04:38:54 GMT
Local: Mon, Nov 27 2000 11:38 pm
Subject: Re: CSV to array
Client-side JavaScript won't open a csv file (or anything other than a
.js file), so the I developed a little tool that might assist you in
converting csv into a two-dimensional JavaScript Array.

Instructions:

Open the csv file in a text processor. Select All and copy.

Run the following converter "app" in a browser and paste the csv data
into the textarea. Click on the convert button.

After a second or so, the original file will be converted into
JavaScript code which "hard-codes" the data into an array. Just copy
this output into your HTML file and your good to go.

Features:
Pre-scans data for quoted material and commas that might be within the
quotes (and subsequently add extra unintential fields) -- converts those
commas into a caret (^), then splits the data by record, then further
splits each record into the fields. Carets are restored to commas for output.

All array members generated are strings. Keep this in mind when dealing
with number values -- use parseInt or parseFloat. All strings are
bounded by single quotes to preserve any original double-quotes.

The fields of Record #0 are the names of the fields.

The number of records can be gotten by (csvData.length - 1) [remember
record 0 is just field names].

This program makes no assumptions about empty fields. The number of
fields will be represented by csvData[recordNum].length.

I wrote this on a Mac. I THINK I took care of the PC newline issue. If
this program only ever returns one long record (a single dimensional
array), let me know.

Hope this helps (code follows),

Fox
***************

<xmp><!--      remove this line to run   -->

<title>CSV to JS Converter</title>
<script language = javascript>

function
csvhandler(f)
{
        var output = "var csvData = new Array();\n";
        var forequote =0;
        var backquote = 0;

        var data = f.data.value;

        forequote = data.indexOf('"', backquote + 1);
                backquote = data.indexOf('"', forequote + 1) + 1;

        while(forequote != -1)
        {

                tdata = data.substring(forequote, backquote);
                tsplit = tdata.split(",").join("^");

                data = data.substring(0, forequote)  + tsplit + data.substring(backquote);
                forequote = data.indexOf('"', backquote + 1);
                backquote = data.indexOf('"', forequote + 1) + 1;
        }

        var tempArray = data.split("\r");

                if(tempArray.length == 1) tempArray = data.split("\n");

        var fields;

        for (var r = 0; r < tempArray.length; r++)
        {
                fields = tempArray[r].split(",");
                output += "csvData[" + r + "] = new Array();\n";
                for (var c = 0; c < fields.length; c++)
                {
                        fields[c] = fields[c].split("^").join(",");
                        output += "csvData[" + r + "][" + c + "] = '" + fields[c] + "';\n";
                }
        }

        f.data.value = output;
        return false;

}

</script>

<body>
<center>
<h1>CSV to JS Converter</h1>
<form name=exchanger onsubmit = "return csvhandler(this)">
<textarea name=data rows = 24 cols = 80></textarea>
<br><br>
<input type=submit value = Convert onclick = "return csvhandler(this.form)">
</form>
</center>
</body>

</xmp><!-- remove this line to run  -->

*************************************************************

Jeff Thies wrote:

>  I'm looking for a reasonable way to turn CSV data into an array. The
> data I have has 20 records per row, some are quoted and some not.

> Seems like there's a lot of potential gotchas!

>   Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.