9 Replies Latest reply on Sep 28, 2018 4:12 AM by Sudha K

    Read CSV File

    Sudha K Level 1

      Hi,

       

           I am using the below function to read csv file. Recently noticed this issue. I did not know why its happening.. is it related to eof reason or any other reason?

       

           When the csv file end of the row is only "F" its not returning by the function. When the empty para or the text para is given to the next line of "F", its returned. Why its happening?? is there is any reason...  How to handle this case?

       

       

           CSV :

      CSV.png    

       

      ReturnValues.png

       

        Empty Next Line (or) next line with contents:

       

      Screen Shot 2018-09-27 at 1.19.00 PM.png

       

        Empty next line:

       

      Screen Shot 2018-09-27 at 1.06.38 PM.png

       

      Next line is bold:

       

      Screen Shot 2018-09-27 at 1.06.59 PM.png

       

       

           Code:

       

      function readFiles(filStr,fileFlag,column1,column2,column3,column4,columnArr){

          var readFil = File(filStr);

          var rCnt = 0;

          if(!readFil.exists){

              return false;

          }else{

              var fileContnts = "";

              readFil.open("r");

              if(fileFlag == "TXT"){

                  fileContnts = readFil.read();

              }else{

                   

                  while(readFil.eof==false){

                      try{

                          zRead=readFil.readln();

                         

                          var csvR = zRead.split(",");

                          if(rCnt != 0){

                              column1.push(csvR[columnArr[0]]);

                              column2.push(csvR[columnArr[1]]);

                              column3.push(csvR[columnArr[2]]);

                              column4.push(csvR[columnArr[3]]);

                              rCnt++;

                          }   

                      }catch(e){}

                      rCnt = 1;

                  }

              }

              readFil.close();

          }

      }

       

       

      Regards,

      Sudha K

        • 1. Re: Read CSV File
          selvam214 Level 1

          Hi,

          Collect the concept from here:

           

          var myFile = File.openDialog("Choose a tab delimitted txt file:"); 

          if (!myFile){exit();}      

           

          var myArray = []  

          myFile.open('r'); 

          while (myFile.eof==false){ 

              var line=myFile.readln().split(","); 

              alert(line[0])

              myArray.push(line[0]); 

              }

           

           

          Selva

          • 2. Re: Read CSV File
            Manan Joshi Level 4

            Hi Sudha,

             

            The issue with your code is that you are using readln and since it reads lines demarcated by newline and the last line does not have a new line after it, that line is skipped.

             

            A better way would be to read the whole file into a string and then split it up on new line character, something like mentioned below.

            var f = new File("Path to your file")
            f.open("r")
            var c = f.read()
            var a = c.split("\n")     //In case the line ends with a new line character you will have a blank line in the last entry. Check it and purge it
            

             

            Hope this helps

             

            -Manan

            • 3. Re: Read CSV File
              Sudha K Level 1

              Hi,

               

                   Thank you for your reply... But my doubt is , if I use the same code for the below case its working.  How it is ??

               

              Input CSV:

              Screen Shot 2018-09-28 at 11.18.24 AM.png

              Result:

               

                   Screen Shot 2018-09-28 at 11.08.50 AM.png

                  

                   Is there is any reason behind the end character is "F"? Because if i append any character with that without any other changes its returning that row contents.  How it is??

               

                  Screen Shot 2018-09-28 at 11.20.22 AM.png

               

                   If its the reason, it should not return the last row contents know? is it right??

               

              - Sudha K

              • 4. Re: Read CSV File
                Manan Joshi Level 4

                You mean to say this new screenshot that you pasted reads all the lines using readln even if the last line is not terminated by a new line? If so send me the file i can have a look

                 

                -Manan

                • 5. Re: Read CSV File
                  Sudha K Level 1

                  Yes...  I don't know the reason for that so only posted to know the reason..

                   

                  I don't know how to attach file here.  I created this file in mas os textEdit.

                  • 6. Re: Read CSV File
                    Manan Joshi Level 4

                    Upload the file on dropbox or gdrive and post the link here.

                    • 7. Re: Read CSV File
                      Sudha K Level 1

                      When u using this kind of csv, is it working correctly for u?

                       

                      Can u pls create csv using textedit ..

                       

                      What is dropbox and post it here.. Can u pls explain the steps?

                      • 8. Re: Read CSV File
                        Manan Joshi Level 4

                        It works as expected and described by me in my first reply. I would like to see your document to understand what you are seeing different. Google dropbox you would find it is

                        • 9. Re: Read CSV File
                          Sudha K Level 1

                          Thank you for your reply..

                           

                          Currently i could not able to access the url for dropbox.  So i could not able to post the csv file.

                           

                          Can i upload the file using forum by attach file?