Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing specific data out of a CSV file

    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!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb Something to try...

    See if you can modify this simulation:
    Code:
    <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!
    Last edited by jmrker; 06-15-2010 at 03:45 PM.

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    One more question

    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

    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".

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Exclamation Something to try...

    Although it does not show in your post, I assume the problem is in your definition:
    Code:
    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:
    Code:
    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

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Could also fix it via
    Code:
        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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    Quote Originally Posted by Old Pedant View Post
    Could also fix it via
    Code:
        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.

  • #7
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Even with removing the spaces, I still can't seem to get it to work.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Okay, debug time. Either use Firefox to look at the values or use alert()s to brute force debug.

    brute force:
    Code:
    ...
      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]; }
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Question Need more information ...

    Quote Originally Posted by gogogadget View Post
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •