...

View Full Version : Parsing specific data out of a CSV file



gogogadget
06-15-2010, 02:30 PM
Hi, I'm trying to write a little Javascript that would parse a CSV file that contains a name and date, and only display the name if the date matches today. Here's some example data:

Name, Date
Joe, 6/10/2010
Jane, 7/11/2010

If today were 6/10/2010, the output of the script would just be "Joe"

Can anybody help? Thanks!

jmrker
06-15-2010, 03:41 PM
See if you can modify this simulation: :rolleyes:


<html>
<head>
<title>CSV &amp; Date parser</title>
<script type="text/javascript">
// From: http://codingforums.com/showthread.php?t=198171

var CSVfile = // simulated text file with \n separating each line
'Name,Date\n'
+ 'Joe,6/14/2010\n'
+ 'Jane,7/11/2010\n';

function showOriginalCSV() {
document.getElementById('TArea').value = CSVfile;
}
function checkCSVdate() {
ckDate = document.getElementById('dinfo').value;

// dt = new Date(); // for current date information instead of data entry
// ckDate = (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear();

var str = '';
var tmp = CSVfile.split('\n');
for (var i=0; i<tmp.length; i++) {
temp = tmp[i].split(',');
if (ckDate == temp[1]) { str += tmp[i]+'<br>'; }
}
if (str == '') { document.getElementById('mtchDiv').innerHTML = 'No matchs found'; }
else { document.getElementById('mtchDiv').innerHTML = 'Found:<br>'+str; }
}

onload = function() {
showOriginalCSV();
}

</script>
</head>
<body>
Original information:<br>
<textarea id="TArea" rows="5"></textarea><p>
<input type="text" id="dinfo" value="6/14/2010"> <button onclick="checkCSVdate()">Check for date</button>
<div id="mtchDiv"></div>
</body>
</html>

Good Luck!
:)

gogogadget
06-15-2010, 04:42 PM
Appreciate your help, but I have one more issue. I've got an external data file in this format

Name,Date \n
George,6/15/2010 \n
George,6/15/2010 \n
George,6/15/2010 \n
George,6/15/2010 \n
Smith,6/11/2010 \n

And here's the code


<html>
<head>
<title>CSV &amp; Date parser</title>
<script type="text/javascript">
// From: http://codingforums.com/showthread.php?t=198171


function checkCSVdate() {
// ckDate = document.getElementById('dinfo').value;

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()

ckDate = (month + "/" + day + "/" + year);

var str = '';
fso = new ActiveXObject("Scripting.FileSystemObject");
fin = fso.OpenTextFile("names2.csv", 1, false, 0);
var line = fin.ReadAll();
var tmp = line.split('\n');
for (var i=0; i<tmp.length; i++) {
temp = tmp[i].split(',');
if (ckDate == temp[1]) { str += tmp[i]; }
}
if (str == '') { document.getElementById('mtchDiv').innerHTML = 'No matches found'; }
else { document.getElementById('mtchDiv').innerHTML = str; }
}

onload = function() {

checkCSVdate();

}

</script>
</head>
<body>
<div id="mtchDiv"></div>
</body>
</html>


Since the data file has a line with 6/15/2010, it should return a name, right? It returns "No Matches Found".

jmrker
06-15-2010, 08:38 PM
Although it does not show in your post, I assume the problem is in your definition:


Name,Date\n
George,6/15/2010 \n
George,6/15/2010 \n
George,6/15/2010 \n
George,6/15/2010 \n
Smith,6/11/2010 \n

It looks like you have a space after the year and before the '\n' character.
The matches must be exact to be found and since the <input> text does not have the space it reports "No matches"
Easiest change might be:


Name,Date \n
George,6/15/2010\n
George,6/15/2010\n
George,6/15/2010\n
George,6/15/2010\n
Smith,6/11/2010\n

Old Pedant
06-15-2010, 10:18 PM
Could also fix it via


if (ckDate == temp[1].replace(/\s/g,"") { str += tmp[i]; }

Another caution: If the text file contains "06/15/2010" (leading zero) then it will not match ckDate.

jmrker
06-15-2010, 10:55 PM
Could also fix it via


if (ckDate == temp[1].replace(/\s/g,"") { str += tmp[i]; }

Another caution: If the text file contains "06/15/2010" (leading zero) then it will not match ckDate.

Nor will it find "7/04/2010" if '7/4/2010" is entered at either location,
but user did not specify any special cases at the time of the answer provided.
:eek:

gogogadget
06-15-2010, 11:03 PM
Even with removing the spaces, I still can't seem to get it to work.

Old Pedant
06-16-2010, 12:11 AM
Okay, debug time. Either use Firefox to look at the values or use alert()s to brute force debug.

brute force:


...
for (var i=0; i<tmp.length; i++) {
temp = tmp[i].split(',');
alert( "ckDate::" + ckDate + "::\ntemp[1]::" + temp[1] + "::");
if (ckDate == temp[1]) { str += tmp[i]; }
...

jmrker
06-16-2010, 03:58 AM
Even with removing the spaces, I still can't seem to get it to work.

In addition to doing the brute force suggestion of 'Old Pedant',
how about posting the code (or a link to the code) you are currently trying to get to work!
Is it significantly different from the post #2?

Saying something does not work is of no help to anyone.
What are the symptoms that don't seem to work for you?
What is your code not doing that you wish it would?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum